1

如何使用 mysqli_stmt 进行 2 个查询?
如果我用一个查询进行它是否有效,但有两个给我这个错误:
“致命错误:在非对象中调用成员函数 execute()。”

<?
$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>";
?>
4

2 回答 2

0

显然,当您调用 prepare 时,其中一个查询(第二个)不会返回对象。这可能是由于查询中的错误。您确定该列已命名imagen吗?

您可以检查结果。如果准备失败,$resul将是错误的。在这种情况下,您可以检查发生了哪个错误。

$resul = $oConni->prepare($cQuery2);
if ($result === false)
{
  echo $oConni->error;
  exit;
}
于 2013-01-09T19:34:20.760 回答
0

试试吧,肯定会奏效的!

//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>";
于 2013-01-09T19:53:55.647 回答