回答

收藏

预处理语句适用于INSERT,但不适用于SELECT

技术问答 技术问答 239 人阅读 | 0 人回复 | 2023-09-12

编辑:解决了,请看下面,    但我还是不知道怎么做才能让它工作
" H; u2 R  x( b; s嗯,我现在被困住了。我有桌子。users:
) P" `9 C! T. e( s: hID int PRIMARY AUTO_INCREMENTEMAIL varchar(60)NICK varchar(60)//...如果我做:4 U0 y  y' Q" n7 j( n, `" {( M5 n
prepare($prepared_statement)) { $id = ""; $stmt->bind_param("iss",$id,$email,$nickname);  $stmt->execute();}?>每次都有效。但是,如果我对的话select执行相同操作:: A7 ?6 ?" a2 Y9 x' t
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!";    >我从未见过万岁!”
1 t, @# b2 n/ \% {2 {3 a但如果我这样改变它:
5 ]' p2 b- F: J  u& N, {$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!";    }}一切都很好。
1 R- [# n8 {( ~* E1 K% a; G: x我在准备好的陈述中犯了一个可怕的错误。但我真的找不到它…我在这里做错了什么?7 J2 m! `( R, L, p$ X9 N1 l
编辑:更新脚本以纠正拼写错误,并添加两行:
. E) ~3 c, l6 q2 f, E% y& J# h. ?echo "maybeid: ".. $maybe_id;echo "maybenick:". $maybe_got_something;页面呼应:
7 A6 o1 @* O% m: }9 P% T        maybeid: maybenick:编辑:工作代码    当我试图调试它时,我明白了这一点:  P. z. \, e/ D3 u/ e' E
$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!";    但是…为什么要担心?
/ R7 c" X9 Q5 U" x8 f/ ]                                                               
3 q, u$ D1 R4 _, p) `6 H4 q  Z    解决方案:                                                               
% g' z) R8 [' C: j1 R                                                                尝试以下操作(从使用)prepare返回语句对象):  [: @+ g, z& v- e: S7 d
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!";   >
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则