I get errors and warnings in mysqli

UPDATE:

I have a few errors and warnings in which I need help in mysqli:

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

In my code below does anyone know how to deal with these warnings and errors?

 $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(); } 
0
source share
3 answers

You need to return the mysqli_result object first , with something like ...

 $res = $stmt->get_result(); 

... then select the number of lines from this object (not $stmt ):

 $numrows = mysqli_num_rows($res); 

UPDATE: get_result is only available in PHP 5.3+; for older versions, 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(); 

There are two recommendations as a side solution: 1) it is probably faster to use one query here and look for the value in both TeacherAlias ​​and TeacherUsername fields at the same time (with the OR operator, for example TeacherAlias = ? OR TeacherUsername = ? ); 2) it would be easier to work with explicitly specified columns ( SELECT id, TeacherAlias AS alias, TeacherUsername AS username... ), and not just ( SELECT * ) in your query.

+2
source

I'm not used to working with msqli, but I will try to answer you, So, first of all, tell the program, "if the query result is 0, then restart everything again. It's not very good, you use resources for nothing. Like raina77ow, you want use

 $res = $stmt->get_result(); 

and this:

 $numrows = mysqli_num_rows($res); 

then you no longer need your condition if ...

0
source

First you need to get the data, or free_result() .

 $stmt->fetch(); 

Then go to mysqli_num_rows()

What would I suggest by skipping the selection if you don't need it:

 $stmt->num_rows; 

This will count the number of rows in your result without having to extract it and clears the cursor.

0
source

All Articles