1

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();
dimlucas
  • 4,822
  • 7
  • 37
  • 51
gareth power
  • 201
  • 2
  • 4
  • 12

3 Answers3

2

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;
}
Devon
  • 32,773
  • 9
  • 61
  • 91
0
$stmt = $connection->prepare($queryA);

returns boolean(false)

make sure your query is correct

you can do a simple check like this

$stmt = $connection->prepare($queryA);
if (!$stmt) {
 echo "failed to run";
} else {
$stmt->bind_param('s',$_POST['userx']);
$stmt->bind_result($name1,$profo,$password1,$key,$id);
$stmt->execute();
$stmt->fetch();
}

Edit: if you are using PDO you were doing it wrong it should be like this

$stmt = $conn->prepare("SELECT name,profo,password,id,key FROM 
collegestudents WHERE email = :email");
    $stmt->bindParam(':email', $email);
Ahmad ghoneim
  • 676
  • 6
  • 13
-1

Change your database connection file with

<?php $con = new PDO('mysql:host=127.0.0.1;dbname=yourdatabasename;','username',''); ?>

Then change below line

$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();

with

$queryA = "SELECT name,profo,password,id,key FROM collegestudents WHERE email = :v";
 $stmt = $connection->prepare($queryA);
 $stmt->execute( array('v' => $_POST['userx']) );
Guspan Tanadi
  • 180
  • 1
  • 12