简体   繁体   English

mysqli准备的语句绑定结果和循环

[英]mysqli prepared statement bind result and loop

I have this code in purpose to search in the database : 我有此代码旨在在数据库中进行搜索:

$key = '%'.$_GET['key'].'%';
$result= $db->prepare("SELECT * FROM information WHERE stuId LIKE ? 
                                            OR stuName LIKE ? LIMIT ?,10");
$result->bind_param('ssi',$key,$key,$startpage);
$result->execute();
$result->bind_result($stuId,$stuName,$date,$stuSex,$stuAdd);
while($result->fetch()) {
    $stuDoB= $date->format('d/m/Y');
    echo "<tr>
              <td class='col1'>
                  <div>$stuId</div>
              </td>
              <td>
                  <div><a href='editStudent.php?stuId=$stuId'>" . htmlspecialchars($stuName) . "</a></div>
              </td>
              <td class='col3'>
                 <div>$stuDoB</div>
              </td>
              <td class='col4'>
                 <div>$stuSex</div>
              </td>
              <td class='col5'>
                 <div>" . htmlspecialchars($stuAdd) . "</div>
              </td>
            </tr>";
}

Im trying to learn prepared statement, so I change from normal way to above code Could you tell me : 我正在尝试学习准备好的语句,因此我从正常方式更改为上述代码,您能告诉我:

  • Do I have to bind_result inside the $result->fetch() or just 1 time bind_result() outside the loop? 我是否必须在$result->fetch() result- $result->fetch()bind_result()还是在循环外仅bind_result()一次bind_result()
  • Is there any problem with my code ? 我的代码有问题吗? It's always give an empty row result with any $key and a fatal error : Fatal error: Call to a member function format() on a non-object in D:\\xampp\\htdocs\\baiTapLon\\showPage.php on line 30 总是给出带有任何$ key和致命错误的空行结果: Fatal error: Call to a member function format() on a non-object in D:\\xampp\\htdocs\\baiTapLon\\showPage.php on line 30

Line 30 is $stuDoB= $date->format('d/m/Y'); 第30行是$stuDoB= $date->format('d/m/Y');

Please help me out. 请帮帮我。

  • Do I have to bind_result inside the $result->fetch() or just 1 time bind_result() outside the loop? 我是否必须在$ result-> fetch()内绑定bind_result还是在循环外仅绑定一次bind_result()?

You have to use bind_result once means outside the loop 您必须在循环外使用一次手段bind_result

  • Is there any problem with my code ? 我的代码有问题吗? It's alway give an empty row result with any $key and a fatal error : "Fatal error: Call to a member function format() on a non-object in D:\\xampp\\htdocs\\baiTapLon\\showPage.php on line 30" 它总是给出带有任何$ key和致命错误的空行结果:“致命错误:在第30行的D:\\ xampp \\ htdocs \\ baiTapLon \\ showPage.php中的非对象上调用成员函数format()”

You can't use format for any variable use it like this 您不能将格式用于任何变量 ,像这样使用它

$date1 = new DateTime($date);
$stuDoB= $date1->format('d/m/Y');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mysqli编写语句,如何循环结果集 - mysqli prepared statement , how to loop the result set bind_result 成一个数组 PHP mysqli 准备语句 - bind_result into an array PHP mysqli prepared statement 将数组绑定到mysqli准备好的语句 - Bind array to mysqli prepared statement mysqli用while循环准备语句 - mysqli prepared statement with while loop 警告:mysqli_stmt_bind_result():绑定变量的数量与准备好的语句中的字段数量不匹配 - Warning: mysqli_stmt_bind_result(): Number of bind variables doesn't match number of fields in prepared statement in mysqli_stmt::bind_result(): 绑定变量的数量与准备好的语句中的字段数量不匹配 - mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in mysqli_stmt_bind_result():绑定变量的数量与准备好的语句中的字段数量不匹配 - mysqli_stmt_bind_result(): Number of bind variables doesn't match number of fields in prepared statement in mysqli_stmt::bind_result():绑定变量的数量与准备语句中的字段数量不匹配 - mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement 没有bind_param的mysqli准备语句 - mysqli prepared statement without bind_param 如何将多个参数绑定到MySQLi准备好的语句 - How to bind multiple parameters to MySQLi prepared statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM