I'm trying to create a login page using php and sql. This is my php code that gets the username and password data from mysql and checks if they are right so the user gets logged in. I'm getting this error:
Call to a member function fetch() on a non-object
on the line:
$row = $result->fetch(PDO::FETCH_ASSOC);
Here is my code:
<?php session_start();
$db = new PDO("mysql:host=localhost;dbname=database","user","pass");
if(mysqli_connect_errno())
{
echo "fail!". mysqli_connect_errnor();
}
$first =$_POST['username'];
$last=$_POST['password'];
$sql="SELECT count(user_id) as records FROM table WHERE username='$first' AND password='$last'";
$result = $db->query($sql);
$row = $result->fetch(PDO::FETCH_ASSOC);
if ($row['records'] == 1);
{
$_SESSION['loggedIN'] =true;
header("Location:logged.php");
die();
}
else {
$_SESSION['loggedIN'] = null;
header("Location:home.php");
die();
}
?>
$_POSTdata directly into the query? Nice...fetch()ing.if($result === FALSE){ var_dump($db->errorInfo()); die; }. P.S. PDO doesn't magically make your queries injection-free, you need to be using prepared statements. P.P.S.mysqli_connect_errnowon't help you here. You're using PDO not MySQLi.