gareth power gareth power - 4 months ago 22
MySQL Question

PHP prepared statement not working

I am making a prepared statement in PHP and my code is fine until I add in 'id' and 'key' to my parameters. They are definitely in the table that I am requesting too. What is wrong? Thanks in advance!


ERROR: Call to a member function bind_param() on boolean


if($_POST['userx']){
echo '<div id="div2"><div id="font2">Dashboard</div>';
$queryA = "SELECT name,profo,password,id,key FROM collegestudents WHERE email = ?";
$stmt = $connection->prepare($queryA);
$stmt->bind_param('s',$_POST['userx']);
$stmt->bind_result($name1,$profo,$password1,$key,$id);
$stmt->execute();
$stmt->fetch();
$stmt->close();

Answer

Key is a reserved keyword in mysql.

It's a good habit to enclose field names and table names in backticks in queries but also to check for errors.

$queryA = "SELECT `name`,`profo`,`password`,`id`,`key` FROM `collegestudents` WHERE `email` = ?";
$stmt = $connection->prepare($queryA);
if ($stmt) {
    $stmt->bind_param('s',$_POST['userx']);
    ...
}
else {
    echo "MySQL ERROR: " . $connection->error;
}
Comments