简体   繁体   中英

Prepared statement works for INSERT but not for SELECT

EDIT: SOLVED, SEE BELOW But still dont know what did I do to make it work :)

OK, I am stuck now. I have table users :

ID int PRIMARY AUTO_INCREMENT
EMAIL varchar(60)
NICK varchar(60)
//...

If I do:

<?php
$email = $_POST["mai"];
$nickname = $_POST["nck"];
$mysqli = new mysqli($db_host, $db_username, $db_password, $database);
$prepared_statement = "INSERT INTO users VALUES(?,?,?)";
if ($stmt = $mysqli->prepare($prepared_statement)) {
 $id = "";
 $stmt->bind_param("iss",$id,$email,$nickname); 
 $stmt->execute();
}
?>

It works every single time. But if I do the same with select:

<?php
 $previous_entries = new mysqli($db_host, $db_username, $db_password, $database);
 $check = $previous_entries->prepare("SELECT ID,NICK FROM users WHERE EMAIL=?;");
 $check->bind_param("s",$email);
 $check->execute();
 $check->bind_result($maybe_id,$maybe_got_something);

  while ($check->fetch()) { //here was typo, but fixed now 
    if ($maybe_got_something==$nickname){
     echo "Hooray!";
    }
  } 

?>

I never ever see the "Hooray!"

But, If I change it like this:

$previous_entries = new mysqli($db_host, $db_username, $db_password, $database);
$prepared_statement = "SELECT ID,NICK FROM users WHERE EMAIL=";
$prepared_statement .=$email;
$result = $previous_entries->query($prepared_statement);
while ($row = $result->fetch_array()){
  if ($row["NICK"]==$nickname){
     echo "Hooray!";
    }
}

Then everything is ok.

I am doing some terrible mistake in prepared statement. But I really really cannot find it... What am I doing wrong here?

EDIT: Updated the script to correct bad typo, and added these two rows:

echo "maybeid: ".. $maybe_id;
echo "maybenick:". $maybe_got_something;

Page echos this:

  maybeid: maybenick: 

EDIT: WORKING CODE When trying to debug it, I got to this:

$previous_entries = new mysqli($db_host, $db_username, $db_password, $database);
$check = $previous_entries->prepare("SELECT ID,NICK FROM users WHERE EMAIL=?;");
$check->bind_param("s",$email);
$check->execute();
$check->bind_result($maybeid,$maybenick);
echo "maybeid: ".$maybeid;
echo "maybenick:". $maybenick;
// got rid of the if statement
  while ($check->fetch()) {
    echo "maybeid: ".$maybeid;
    echo "maybenick:". $maybenick;
    if ($maybenick==$nickname){

     echo "Hooray!";
    }
  } 

But ... why is it worrking? :)

准备好的查询是$check但是在获取时您正在使用$stmt

Try this (use a statement object returned from prepare):

<?php
 $previous_entries = new mysqli($db_host, $db_username, $db_password, $database);
 $check = $previous_entries->prepare("SELECT ID,NICK FROM users WHERE EMAIL=?;");
 $check->bind_param("s",$email);
 $check->execute();
 $check->bind_result($maybe_id,$maybe_got_something);
 if($check->num_rows > 0){
  while ($check->fetch()) {
    if ($maybe_got_something==$nickname){
     echo "Hooray!";
    }
  } 
}
?>

Try to change $stmt->fetch() with this one $check->fetch()

You could probably need to set your error_reporting level to something like

error_reporting = E_ALL & ~E_NOTICE

in your php.ini for development enviroment to avoid this kind of mistakes.

Regards

You must change your code like this:

...
$check = $previous_entries->prepare("SELECT ID,NICK FROM users WHERE EMAIL=:email;");
$check->bind_param(":email",$email);
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM