【问题标题】:php sql with multiple selects not working具有多个选择的php sql不起作用
【发布时间】:2014-07-25 19:31:09
【问题描述】:

以下代码给了我这个错误:

在第 21 行调用未定义的方法 mysqli_stmt::get_result()。

我不明白这个对象是如何工作的,为什么我可以进行第一次数据库调用但不能进行第二次。

    <?php 

    header('Content-Type: application/json');
    include_once 'do_dbConnect.php';
    include_once 'functions.php';
    sec_session_start(); 

    //identify who took the last call
    $stmt =  $mysqli->stmt_init();
    if ($stmt->prepare("SELECT MAX(dateOfCall), id FROM call")) { //setup the query statement
      $stmt->execute(); //execute the statement
      $result = $stmt->get_result(); //get the results
      $row = $result->fetch_assoc(); //get the first row
      $user_id = $row['id']; //get the id column
    }


    //identify how many team members there are
    if ($stmt->prepare("SELECT id FROM teamMembers")) { //setup the query statement
      $stmt->execute(); //execute the statement
      $result = $stmt->get_result(); //get the results
      $memberCount = $result->num_rows;
    }


    //get next user
    if ($stmt = $mysqli->prepare("SELECT * FROM teamMembers WHERE id = (? + 1) % ?")) { //setup the query statement
      $stmt->bind_param('ii', $user_id, $memberCount);
      $stmt->execute(); //execute the statement
      $result = $stmt->get_result(); //get the results
      $row = $result->fetch_assoc(); //get the first row
      $next_user_id = $row['id']; //get the id column
      $next_user_name = $row['username'];
    }
    $stmt->close();
    //get the next call taker from the teamMember table

    echo json_encode($row);

    ?> 

【问题讨论】:

  • 请指定哪一行是21??你怎么知道第一个mysqli调用没问题?
  • 我不知道您的问题的答案,但我始终发现了解某事的工作原理通常是最重要的一步。看到这条评论:stackoverflow.com/a/8343970/1888402 另外,不要害羞,这是一个公共论坛,人们喜欢通过帮助别人获得积分,所以这对所有参与者来说都是双赢的。
  • 一个问题是你的第一个查询 callMysql reserved keyword 你需要用 bact-ticks 转义它
  • 如上所述,call 是保留字。使用反引号` 或将其重命名为calls。可以很好地解决问题。

标签: php mysql


【解决方案1】:

请阅读此方法的用户说明:

http://php.net/manual/en/mysqli-stmt.get-result.php

它需要 mysqlnd 驱动程序...如果您的网站空间上没有安装它,您将不得不使用 BIND_RESULT & FETCH!

http://www.php.net/manual/en/mysqli-stmt.bind-result.php

http://www.php.net/manual/en/mysqli-stmt.fetch.php

【讨论】:

    猜你喜欢
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多