dongwei5794 2012-08-23 22:46
浏览 35
已采纳

我在mysqli中收到错误和警告

UPDATE:

I have a couple of errors and warnings I need help with in mysqli:

Fatal error: Call to undefined method mysqli_stmt::get_result() in ... on line 63

In my code below does anyone know how these warnings and errors can be dealt with?

$query = "SELECT * FROM Teacher WHERE TeacherAlias = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s",$getid);
// execute query
$stmt->execute();  
//get results
$result = $stmt->get_result(); 

$numrows = mysqli_num_rows($result);
if ($numrows == 0){   

       // don't use $mysqli->prepare here
$query = "SELECT * FROM Teacher WHERE TeacherUsername = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s",$getuser);
// execute query
$stmt->execute(); 

}
  • 写回答

3条回答 默认 最新

  • dsmlf1207915 2012-08-23 22:55
    关注

    You need to retrieve a mysqli_result object first, with something like...

    $res = $stmt->get_result();
    

    ... then fetch the number of rows from this object (not $stmt):

    $numrows = mysqli_num_rows($res);
    

    UPDATE: get_result method is available in PHP 5.3+ only, for the older versions one should use the following approach:

    // $stmt preparing code goes here...
    
    $stmt->execute();
    $stmt->store_result();
    $num_rows = $stmt->num_rows;
    doSomethingWith($num_rows);
    
    // processing cycle:
    $stmt->bind_result($some_param, $another_param);
    while ($stmt->fetch()) {
       doSomethingElseWith($some_param, $another_param);
    }
    $stmt->free_result();
    $stmt->close();
    

    As a sidenote, two recommendations: 1) it'd be probably faster to use a single query here and look for value both in TeacherAlias and TeacherUsername fields simultaneously (with OR operator, like TeacherAlias = ? OR TeacherUsername = ?); 2) it'd be easier to work with explicitly stated columns (SELECT id, TeacherAlias AS alias, TeacherUsername AS username...) instead of just (SELECT *) in your query.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!