gpt4 book ai didi

php - 我在 mysqli 中收到错误和警告

转载 作者:行者123 更新时间:2023-11-29 08:47:51 25 4
gpt4 key购买 nike

更新:

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

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

在我下面的代码中,有人知道如何处理这些警告和错误吗?

$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首先对象,例如...

$res = $stmt->get_result();

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

$numrows = mysqli_num_rows($res);

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

// $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 别名、TeacherUsername AS 用户名...)会更容易,而不仅仅是 (SELECT *)您的查询。

关于php - 我在 mysqli 中收到错误和警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12101053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com