简体   繁体   English

我在mysqli中收到错误和警告

[英]I am getting errors and warnings in mysqli

UPDATE: 更新:

I have a couple of errors and warnings I need help with in mysqli: 我在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(); 

}

You need to retrieve a mysqli_result object first, with something like... 您需要首先检索mysqli_result对象,例如......

$res = $stmt->get_result();

... then fetch the number of rows from this object (not $stmt ): ...然后从此对象获取行数(而不是$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: 更新: 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();

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

I'm not used to work with msqli but I will try to answer you, So first of all, yo say the program, "if the result of the request is equal to 0 then restart all again. This is not very good, you're using resources for nothing. Like raina77ow said, you want to use 我不习惯和msqli一起工作,但我会尽力回答你,所以首先,你说程序,“如果请求的结果等于0,那么重新启动。这不是很好,你“无所事事地使用资源。就像raina77ow所说,你想要使用

$res = $stmt->get_result();

and this: 和这个:

$numrows = mysqli_num_rows($res);

then you don't need anymore your condition if... 如果......你不再需要你的条件了......

You need to fetch your data first, or free_result() . 您需要先获取数据,或者free_result()

$stmt->fetch();

Then pass this to mysqli_num_rows() 然后将其传递给mysqli_num_rows()

What i would suggest is, skipping the fetch, if you don't need it and doing: 我建议的是,跳过抓取,如果你不需要它并做:

$stmt->num_rows;

This will count the number of rows in your result without needing to fetch it, and clears the cursor. 这将计算结果中的行数而无需获取它,并清除游标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM