doupai6875 2014-01-29 14:39
浏览 72
已采纳

Prepared语句适用于INSERT但不适用于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? :)

  • 写回答

4条回答 默认 最新

  • dream_high1026 2014-01-29 14:52
    关注

    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!";
        }
      } 
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题