webdevRefinery Forum: Stubborn PDO Code! - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

User is offline goldenpages 

  • Group: Members
  • Posts: 46
  • Joined: 03-August 12
  • Expertise:HTML,PHP

Posted 03 August 2012 - 12:51 PM (#1)

Stubborn PDO Code!


Is there something that I missed, for PHP to ignore my if statements?
Hi. I'm connecting to my app with PDO now and i would like to show you guys some code that's been not working when tested:

login.php...

<?php

try 
{
$conn = new PDO("mysql:dbname=users;host=localhost", "register", "mysql" );
echo "Successful Connection ";
}

catch(PDOException $e)
{
echo $e->getMessage();
}

$sql = $conn->prepare("SELECT * FROM users WHERE Password = '$password' AND Email = '$email'");
$sql->query();
$count = $sql->rowCount();


if ($sql->rowCount() < 1)
{
header("Location: signin.php");
}

if ($sql->rowcount() > 0)
{
session_start();
$_SESSION['login'] = 1;
$_SESSION['email'] = $_POST["email"];
header("Location: home.php");
}



$conn = null;

?>



I need it to return with a number 1 or 0 rows so the browser knows what to do, via if statements, is this way right or wrong?
0


User is online Hyde 

  • Group: Members
  • Posts: 1580
  • Joined: 08-March 10

Posted 03 August 2012 - 12:59 PM (#2)

What is the output of
$sql->rowcount()
?

Also, I notice that you are injecting the variables directly into the query. In PDO, the prepare method is made just for that, but in a safer and arguably nicer way :

$sql = $conn->prepare("SELECT * FROM users WHERE Password = '$password' AND Email = '$email'");
$sql->query();


$sql = $conn->prepare('SELECT * FROM users WHERE Password = :password AND Email = :email');
$sql->execute(array('password' => $password, 'email' => $email));


That's how
prepare()
is meant to be used. Otherwise, you just use
query()
.
Hyde | HTML & CSS | PHP & SQL | Objective-C | Java | Basic JavaScript
1


User is offline Cyril 

  • Group: Members
  • Posts: 2545
  • Joined: 03-August 10
  • Expertise:HTML,CSS,PHP,Javascript,Graphics

Posted 03 August 2012 - 01:00 PM (#3)

Please, indent your code ;) It will make reading through it much easier.

What error does it give you, or where does it simply fail? I'm guessing at the prepare statement:

$sql = $conn->prepare("SELECT * FROM users WHERE Password = '$password' AND Email = '$email'");
$sql->query();

should be
$sql = $conn->prepare("SELECT * FROM users WHERE Password = ? AND Email = ?");
$sql->execute(array($password, $email));


See here:
http://webdevrefiner...sql-code-sucks/

website :: github :: twitter :: dribbble :: forrst
html, css, php, javascript, graphics
0


User is offline goldenpages 

  • Group: Members
  • Posts: 46
  • Joined: 03-August 12
  • Expertise:HTML,PHP

Posted 03 August 2012 - 01:05 PM (#4)

It gives no output, thats why my if statements don't execute

So how would I output the number of rows using the SELECT statement in PDO, so I can use if statements with it?
0


User is online Hyde 

  • Group: Members
  • Posts: 1580
  • Joined: 08-March 10

Posted 03 August 2012 - 01:07 PM (#5)

View Postgoldenpages, on 03 August 2012 - 01:00 PM, said:

It gives no output, thats why my if statements don't execute

It looks like rowCount doesn't actually return a numerical value. That would explain why your if statements are being executed.

I think the best way to look for an existing user matching an $email and $password would be this :

check post below

That way, there's is no playing with variable types. If something is fetched, the if statement will return true and the user will proceed to be signed it.
Hyde | HTML & CSS | PHP & SQL | Objective-C | Java | Basic JavaScript
0


User is offline goldenpages 

  • Group: Members
  • Posts: 46
  • Joined: 03-August 12
  • Expertise:HTML,PHP

Posted 03 August 2012 - 01:12 PM (#6)

I inserted that code and that way returns me true even if its an unexisting email or password.
0


User is online Hyde 

  • Group: Members
  • Posts: 1580
  • Joined: 08-March 10

Posted 03 August 2012 - 01:24 PM (#7)

View Postgoldenpages, on 03 August 2012 - 01:12 PM, said:

I inserted that code and that way returns me true even if its an unexisting email or password.

Shouldn't. That's how Kyek told me to check for existing record and it has always worked fine for searching a SQL database for content. Are the variables both set correctly?

Also, a few tips :

  • Indent your code (as Cyril said).
  • Using two
    '
    instead of two
    "
    when you want to make a string in which you know will not be any variables is better.
    echo "$variable";
    will display $variable's value, while
    echo '$variable';
    will display, character for character,
    $variable
    . This is because PHP doesn't scan strings within two
    '
    for variables, so that's faster when you know there aren't any variable in there.
  • SQL row names should, just like a PHP variable's name, not start with a capital letter.
  • Don't hesitate to ask if you don't understand something, either that we said or in the code that we showed you.

Hyde | HTML & CSS | PHP & SQL | Objective-C | Java | Basic JavaScript
1


User is offline goldenpages 

  • Group: Members
  • Posts: 46
  • Joined: 03-August 12
  • Expertise:HTML,PHP

Posted 03 August 2012 - 01:52 PM (#8)


<?php

try 
{
$conn = new PDO("mysql:dbname=users;host=localhost", "register", "mysql" );
echo "Successful Connection ";
}

catch(PDOException $e)
{
echo $e->getMessage();
}

$sql = $conn->prepare("SELECT * FROM users WHERE Password = :password AND Email = :email");

$sql->bindParam(":email",$email); 
$sql->bindParam(":password",$password); 
 
$sql->execute(); 

$count = $sql->rowCount(); 
 
 
 

if ($count < 1)
{
header("Location: signin.php");
}

if ($count == 1) 
{
session_start();
$_SESSION['login'] = 1;
$_SESSION['email'] = $_POST["email"];
header("Location: home.php");
}

?>


0


User is online Hyde 

  • Group: Members
  • Posts: 1580
  • Joined: 08-March 10

Posted 03 August 2012 - 03:12 PM (#9)

Why did you decided to go why individual
bindParam
instead of one
execute
which does everything?

Also, I just found what was wrong with the previous code I gave you. It should be the other way around :

if($check = $sql->fetchObject())
{
session_start();
$_SESSION['login'] = 1;
$_SESSION['email'] = $_POST['email'];
header("Location: home.php");
}

else
{
header("Location: signin.php");
}


instead of

if($check = $sql->fetchObject())
{
header("Location: signin.php");
}

else
{
session_start();
$_SESSION['login'] = 1;
$_SESSION['email'] = $_POST["email"];
header("Location: home.php");
}


This is because, in your code,
if ($count == 1)
is equal to
if($count == true)
or simply
if($count)
. This is also what
if($check = $sql->fetchObject())
does, except it doesn't rely on the numericallity of a value, and for this reason is better in my opinion.

I also imagine that simply performing a
fetchObject
would be easier/quicker than a
rowCount
.

If others have anything to say, it would help me :D

_____________________________________________________________________________________

Edit : Try this :

<?php
try 
{
$conn = new PDO('mysql:dbname=users;host=localhost', 'register', 'mysql');
echo 'Successful Connection';
}

catch(PDOException $e)
{
echo $e->getMessage();
}

$sql = $conn->prepare('SELECT * FROM users WHERE Password = :password AND Email = :email');
$sql->execute(array('password' => $password, 'email' => $email));

if($check = $sql->fetchObject())
{
session_start();
$_SESSION['login'] = 1;
$_SESSION['email'] = $_POST['email'];
header('Location: home.php');
}

else
{
header('Location: signin.php');
}
?>


And I just saw something. Do NOT select all fields (*) when you do perform a
SELECT
, especially not if you only want to make sure the user is existing. Just select one, even if you don't have any use for it.

Also, notice that I've replaced basically every
"
for a
'
.
Hyde | HTML & CSS | PHP & SQL | Objective-C | Java | Basic JavaScript
1


User is offline goldenpages 

  • Group: Members
  • Posts: 46
  • Joined: 03-August 12
  • Expertise:HTML,PHP

Posted 03 August 2012 - 04:59 PM (#10)

does the code I did, somehow prevent SQL injections? if so, how?
0


User is online Hyde 

  • Group: Members
  • Posts: 1580
  • Joined: 08-March 10

Posted 04 August 2012 - 02:08 PM (#11)

View Postgoldenpages, on 03 August 2012 - 04:59 PM, said:

does the code I did, somehow prevent SQL injections? if so, how?

Using prepared PDO statements is enough to prevent SQL injections. It could still be a good idea to make sure that the data given by the user is what it is expected to be, though. For example, that an "email" is really an email.
Hyde | HTML & CSS | PHP & SQL | Objective-C | Java | Basic JavaScript
0


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services