Reputation: 222
I have found same question at SO
Prepared statement works for INSERT but not for SELECT
and tried to follow it,but no luck.One more thing, that question is on OOP
structure,but my one is procedural style
Here is my code
$stmt = mysqli_stmt_init($connect_dude);
if(mysqli_stmt_prepare($stmt,"SELECT headx FROM main_page WHERE user_id=?")){
mysqli_stmt_bind_param($stmt, "i", $idx);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $head);
printf("Number of rows: %d.\n", mysqli_stmt_num_rows($stmt));
...................
...................
}
It shows Number of rows:0
where it should be 1
.That's why,I cannot proceed further to fetch data
from database
.I tried my best to fix the problem,but couldn't make it.
Your help would be really appreciated.
Thanks for your time.
Upvotes: 1
Views: 266
Reputation: 222
I have figured it out.
Need to use mysqli_stmt_store_result($stmt)
mysqli_stmt_bind_param($stmt, "i", $idx);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
printf("Number of rows: %d.\n", mysqli_stmt_num_rows($stmt));
gives me Number of rows:1
As of php.net
You must call mysqli_stmt_store_result() for every query that successfully produces a result set (SELECT, SHOW, DESCRIBE, EXPLAIN)
BUT
NOT MUST for UPDATE
,DELETE
AND INSERT
.
Thanks everyone.
Upvotes: 1