我在mysqli中收到错误和警告 [英] I am getting errors and warnings in mysqli

查看:47
本文介绍了我在mysqli中收到错误和警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:

我在mysqli中需要帮助的一些错误和警告:

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(); 

}

推荐答案

您需要获取 mysqli_result 对象首先出现,类似...

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

$res = $stmt->get_result();

...然后从该对象(不是$stmt)获取行数:

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

$numrows = mysqli_num_rows($res);

更新:get_result方法仅在PHP 5.3+中可用,对于较旧的版本,应使用以下方法:

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();

作为旁注,有两个建议:1)在这里使用单个查询并同时在TeacherAlias和TeacherUsername字段中查找值(使用OR运算符,例如TeacherAlias = ? OR TeacherUsername = ?)可能更快. 2)使用明确声明的列(SELECT id, TeacherAlias AS alias, TeacherUsername AS username...)而不是查询中的(SELECT *)会更容易.

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.

这篇关于我在mysqli中收到错误和警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆