MYSQLi store result and then get result

拟墨画扇 提交于 2020-05-02 05:00:13

问题


I have a function which does the following things

     $stmt = mysqli_prepare..
         //check for preparation failure
     mysqli_stmt_bind_param
         //Check for bind error
     mysqli_stmt_execute
         //Check for execution error

     mysqli_stmt_store_result   //Problem is here
         //Check for error
     return $stmt

I have these steps in a function because I need it in two placeds the same piece of code. After getting the stmt, I need to check number of rows [mysqli_Stmt_num_rows]. So only I have that stored result stmt at the end of the function.

After checking the rows, i need to get a column value from the result. So I tried to execute mysqli_stmt_get_result on the returned statement object. It fails.

I just removed the store result part and just returned the executed stmt from the function. Get result works then. But I cannot count rows until I store the result.

Then I have done simple trick that tried to clone the returned statement. Unfortunately, mysqli stmt is not clonable.

How do I solve this issue?


回答1:


mysqli_result class also has a property called num_rows

$result = $stmt->get_result();
echo $result->num_rows;

If you want to stick to procedural style, which I do not recommend, there is a corresponding function mysqli_num_rows($result)



来源:https://stackoverflow.com/questions/33315481/mysqli-store-result-and-then-get-result

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!