What does mysqli_store_result () really do?

I want to understand what What mysqli_store_result is really? When I visited the mysqli_store_result PHP mysqli_store_result , I found a definition

 mysqli_store_result — Transfers a result set from the last query 

Question: where does it transfer the result set? Actually, I got the error "Commands out of sync; you can't run this command now" after running mysqli_multi_query But when I used the following method, the error mysqli_multi_query away.

  mysqli_multi_query($connection,$query); do { mysqli_store_result($connection); } while(mysqli_next_result($connection)); 

Now, should I use these mysqli_store_result($connection) and mysqli_next_result($connection) after each mysqli_query or right after mysqli_multi_query since I read in PHP Manaul that

"Although it is always useful to free the memory used by the query result using the mysqli_free_result () function, when transferring large result sets using mysqli_store_result () this becomes especially important."

Source: PHP: mysqli_store_result

Another question arises when I executed the above mysqli_multi_query($connection,$query); , I set the echo 'storing result <br />' operator echo 'storing result <br />' , as shown below

 do { echo 'storing result <br /> mysqli_store_result($connection); } while(mysqli_next_result($connection)); 

Although there were only two INSERT requests in the $ request, but it gave the following output

 storing result storing result storing result storing result 

This means that four result sets have been transmitted. I can not understand this situation. And the last question. Does the above do while process do while performance?

+6
source share
2 answers

Previous comments stated that mysqli_store_result should not be used with INSERT statements, but no one mentioned the corresponding function: mysqli_affected_rows (). If your statement returns a recordset and you want to check it numerically, use mysqli_num_rows ().

If you are dealing with a mixture, you can start:

 $queries[]="INSERT INTO TestTable (Column1) VALUES ('TEST1')"; $queries[]="SELECT * FROM TestTable WHERE Column1 LIKE 'TEST%'"; $queries[]="INSERT INTO TestTable (Column1) VALUES ('TEST2')"; $queries[]="SELECT * FROM TestTable WHERE Column1 LIKE 'TEST%'"; $queries[]="DELETE FROM TestTable WHERE Column1 LIKE 'TEST%'"; if(mysqli_multi_query($con,implode(';', $queries))){ do{ if($result=mysqli_store_result($con)){ echo "Selected rows = ".mysqli_num_rows($result)."<br><br>"; mysqli_free_result($result); }else{ $cumulative_rows+=$aff_rows=mysqli_affected_rows($con); echo "Current Query Affected Rows = $aff_rows, Cumulative Rows = $cumulative_rows<br><br>"; } } while(mysqli_more_results($con) && mysqli_next_result($con)); } 

Outputs:

Current queries affected by rows = 1, cumulative affected rows = 1

Selected Rows = 1

Current query affected by rows = 1, cumulative affected rows = 2

Selected Rows = 2

Current queries affected by rows = 2, cumulative affected rows = 4

+5
source

It will actually select the entire result set from MySQL. Then you can mysqli_data_seek () go to a specific line inside the set. Thus, all results will be stored on the php side after the first call, and subsequent calls will simply request results from php

0
source

Source: https://habr.com/ru/post/944187/


All Articles