如何使用mysqli_stmt进行2次查询

时间:2013-01-09 19:29:54

标签: php mysql mysqli

如何使用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>";
?>

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分: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>";