how to make 2 queries with mysqli_stmt

2019-06-02 09:19发布

问题:

How I can make 2 queries with mysqli_stmt?
if I make it with one query if it works, but with two gives me this error:
"Fatal error: Call to a member function execute() on a non-object in."

<?
$oConni=new mysqli('localhost', 'user', 'password', 'database');

$cQuery = "SELECT email, firstname, profile_image FROM usersg";
$stmt = $oConni->prepare($cQuery);
$stmt->execute();
$stmt->bind_result($email, $name, $imagen);

$cQuery2 = "SELECT oauth_uid, oauth_token, username, imagen FROM users";
$resul = $oConni->prepare($cQuery2);
$resul->execute();
$resul->bind_result($id, $fich, $nameTwi, $imagenTwi);

echo "<table border='1'>";
while ($stmt->fetch()) {
echo "<tr>
<td>" . $email. "</td>
<td>" . $name. "</td>
<td><img src='".$imagen."' width=40px height=40px></td>
</tr>";
}
echo "</table><p>";
echo "<table border='1'>";
while ($resul->fetch()) {
echo "<tr>
<td>" . $id. "</td>
<td>" . $fich. "</td>
<td>" . $nameTwi. "</td>
<td><img src='".$imagenTwi."' width=40px height=40px></td>
</tr>";
}
echo "</table>";
?>

回答1:

thry it, surely it will work !

//First Query
$cQuery = "SELECT email, firstname, profile_image FROM usersg";
$stmt = $oConni->prepare($cQuery);
$stmt->execute();
$stmt->bind_result($email, $nombre, $imagen);
echo "<table border='1'>";
 while ($stmt->fetch()) {
echo "<tr>
<td>" . $email. "</td>
<td>" . $nombre. "</td>
<td><img src='".$imagen."' width=50px height=50px></td>
</tr>";
}
echo "</table>";

//Second Query
$cQuery2 = "SELECT oauth_uid, oauth_token, username, imagen FROM users";
$resul = $oConni->prepare($cQuery2);
$resul->execute();
$resul->bind_result($id, $fichero, $nombreTwitter, $imagenTwitter);
echo "<table border='1'>";
while ($resul->fetch()) {
echo "<tr>
<td>" . $id. "</td>
<td>" . $fichero. "</td>
<td>" . $nombreTwitter. "</td>
<td><img src='".$imagenTwitter."' width=50px height=50px></td>
</tr>";
}
echo "</table>";


回答2:

Apparently one of the queries (the second one) doesn't return an object when you call prepare. That's probably due to an error in the query. Are you sure the column is named imagen?

You can check the result. If preparing failed, $resul will be false. In that case, you can check which error occurred.

$resul = $oConni->prepare($cQuery2);
if ($result === false)
{
  echo $oConni->error;
  exit;
}


标签: php mysql mysqli