dran0703 2016-05-13 05:26
浏览 77
已采纳

mysqli prepare()返回一个布尔值true而不是一个语句对象

I've seen this issue all over the internet but none of the solutions for others have fixed my errors. I have the following PHP code:

  $err = false;
  $mysqli = getMysqlConnection();
  if ($stmt = $mysqli->prepare("INSERT INTO users (username, email, regtime, emailverified, type) values (?,?,?,?,?);"))
  {
      date_default_timezone_set("America/New_York");
      $dateStr = date("m-d-Y h:i:s");
      $emailverified = 0;
      $type = 0;

      $stmt->bind_param('sssii', $username, $email, $dateStr, $emailverified, $type);
      $rc = $stmt->execute();
      $err = $rc ? $err : true;

      $userId = $stmt->insert_id;    

      if ($stmt = $mysqli->prepare("INSERT INTO usercreds (user_id, username, hash) VALUES (?,?,?);") && $err === false)
      {
          //
          //right here, for some reason, $stmt is true (boolean), not an object
          //

          $stmt->bind_param('iss', $userId, $username, $hash);
          $rc = $stmt->execute();
          $err = $rc ? $err : true;
      }
      else {
        $err = true;
      }
  }

The first query runs without problems, but the second query breaks on the call to bind_params. I get the error

Fatal error: Uncaught Error: Call to a member function bind_param() on boolean

This means (and I have confirmed) that the second time I do $stmt = $mysqli->prepare(), it's returning a boolean rather than an object. Furthermore, it's returning true, which I find very strange.

I have quadruple checked my query, and it is perfectly fine and valid. Yet for some reason the second time I try to prepare a statement he prepare method returns a boolean instead of a statement object.

Another note, echoing out $mysqli->error after the second prepare() outputs nothing.

How do I resolve this issue?


Afterthought:

Part of me feels like this is because I'm using the same mysqli object twice in a row? or maybe because I'm using the same statement object twice in a row? Is there some sort of "shutting down" or "resetting" of one of these objects that needs to take place in order to execute a second query?

Another edit:

Additional proof that my second query is valid. I copied and pasted it and replaced the ?'s with values, executed it in mysql workbench, and it ran perfectly fine.

INSERT INTO usercreds (user_id, username, hash) VALUES (1234567890,'testusr','hashhashhash');
  • 写回答

2条回答 默认 最新

  • duanbin198788 2016-05-13 07:08
    关注

    Your problem is caused by essentially wrong way of error handling. That is, beside producing errors, is making your code bloat.

    Below is the only code you really need:

    date_default_timezone_set("America/New_York");
    // below is the only line you need for the error handling
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $mysqli = getMysqlConnection();
    
    $dateStr = date("m-d-Y h:i:s");
    $emailverified = 0;
    $type = 0;
    
    $stmt = $mysqli->prepare("INSERT INTO users (username, email, regtime, emailverified, type) values (?,?,?,?,?)");
    $stmt->bind_param('sssii', $username, $email, $dateStr, $emailverified, $type);
    $stmt->execute();
    $userId = $mysqli->insert_id;
    
    $stmt = $mysqli->prepare("INSERT INTO usercreds (user_id, username, hash) VALUES (?,?,?)");
    $stmt->bind_param('iss', $userId, $username, $hash);
    $stmt->execute();
    

    The rest is depends on what you going to do in case of error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥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