Mysqli_stmt_prepare returns different results to mysqli_query

In my Profiles table I have 5 ID numbers, as shown below…

I query this table with mysqli_stmt_prepare, then again with mysqli_query

$query = "SELECT profile_id FROM Profiles";

$stmt = mysqli_prepare($dbh, $query);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $profile);
mysqli_stmt_fetch($stmt); 
mysqli_stmt_store_result($stmt);

echo "<p>Running: " . $query . "</p>";
echo "<p>Results from mysqli_stmt:</p>";

while (mysqli_stmt_fetch($stmt)) {
    echo $profile . "<br />\n";
}


echo "<p>Running: " . $query . "</p>";
echo "<p>Results from mysqli_query:</p>";

$q = mysqli_query($dbh, $query);
while ($row = mysqli_fetch_array($q))
{
    echo $row['profile_id'] . "<br />\n";
}

And here are the results…

Running: SELECT profile_id FROM Profiles

Results from mysqli_stmt:

2
3
4
5

Running: SELECT profile_id FROM Profiles

Results from mysqli_query:

1
2
3
4
5

Why does row 1 not result when running from mysqli_stmt, but does from mysqli_query?

P.S. Before anyone jumps in, please don’t tell me I don’t need to be using mysqli_stmt_prepare for this statement, I know. I have just simplified the query for this demo.

Get rid of the the fetch.

2 Likes

Precisely - your first fetch() is taking the first result, but you don’t do anything with it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.