准备好的语句返回0行,普通查询返回1行

时间:2012-03-25 06:44:19

标签: php enums mysqli prepared-statement

我有这个问题,其中预准备语句返回0行,而普通查询返回1行

这是准备好的陈述

$sender = 'marshall';
$accepter = 'LUcase';
$int1 = '1';
$check_request_1 = $friend_zone->prepare("SELECT * FROM friends_request WHERE (sender = ? and accepter = ? and status = ?) OR (accepter = ? and sender = ? and status = ?)");
$check_request_1->bind_param('ssssss', $sender, $accepter, $int1, $sender, $accepter, $int1);
$check_request_1->execute();
$check_request_1->store_result();
$nrow2 = $check_request_1->num_rows;
$check_request_1->fetch();
$check_request_1->close();

echo $nrow2

是1是字符串,因为该字段是数据库中的枚举

普通查询

$sql = "SELECT * FROM friends_request WHERE (accepter = 'marshall' AND sender = 'LUcase' AND status = '1') OR (accepter = 'LUcase' AND sender = 'marshall' AND status = '1')";
$query = mysqli_query($friend_zone, $sql);
$numrow = mysqli_num_rows($query);

整个代码(这可能是问题,希望能在几分钟内解决)

  $check_request = $friend_zone->prepare("SELECT * FROM friends_request WHERE (sender = ? and accepter = ? and status = ?) OR (accepter = ? and sender = ? and status = ?)");
  $check_request_1 = $friend_zone->prepare("SELECT * FROM friends_request WHERE (sender = ? and accepter = ? and status = ?) OR (accepter = ? and sender = ? and status = ?)");
  $check_request_2 = $friend_zone->prepare("SELECT * FROM friends_request WHERE accepter = ? and sender = ? and status = ?");

  $check_request->bind_param('ssssss', $sender, $accepter, $int0, $sender, $accepter, $int0);
  $check_request_1->bind_param('ssssss', $sender, $accepter, $int1, $sender, $accepter, $int1);
  $check_request_2->bind_param('sss', $sender, $accepter, $int0);

  $check_request->execute();
  $check_request_1->execute();
  $check_request_2->execute();

  $check_request->store_result();
  $check_request_1->store_result();
  $check_request_2->store_result();

  $check_request->bind_result($reqid, $req_sender, $req_accepter, $req_status);
  $check_request_1->bind_result($reqid1, $req_sender1, $req_accepter1, $req_status1);
  $check_request_2->bind_result($reqid2, $req_sender2, $req_accepter2, $req_status2);

  $nrow1 = $check_request->num_rows;
  $nrow2 = $check_request_1->num_rows;
  $nrow3 = $check_request_2->num_rows;

  $check_request->fetch();
  $check_request_1->fetch();
  $check_request_2->fetch();

  $check_request->close();
  $check_request_1->close();
  $check_request_2->close();

2 个答案:

答案 0 :(得分:0)

更新:看到评论后,很明显两个条件都不一样。所以这是你的代码的修复。

$sender = 'marshall';
$accepter = 'LUcase';
$int1 = '1';
$check_request_1 = $friend_zone->prepare("SELECT * FROM friends_request WHERE (sender = ? and accepter = ? and status = ?) OR (accepter = ? and sender = ? and status = ?)");
$check_request_1->bindParam(1, $sender);
$check_request_1->bindParam(2, $accepter);
$check_request_1->bindParam(3, $int1 );
$check_request_1->bindParam(4, $accepter);
$check_request_1->bindParam(5, $sender);
$check_request_1->bindParam(6, $int1);
$check_request_1->execute();
$check_request_1->store_result();
$nrow2 = $check_request_1->num_rows;
$check_request_1->fetch();
$check_request_1->close();

echo $nrow2

答案 1 :(得分:0)

更改

  $check_request->execute();
  $check_request_1->execute();
  $check_request_2->execute();

  $check_request->store_result();
  $check_request_1->store_result();
  $check_request_2->store_result();

  $check_request->bind_result($reqid, $req_sender, $req_accepter, $req_status);
  $check_request_1->bind_result($reqid1, $req_sender1, $req_accepter1, $req_status1);
  $check_request_2->bind_result($reqid2, $req_sender2, $req_accepter2, $req_status2);

  $nrow1 = $check_request->num_rows;
  $nrow2 = $check_request_1->num_rows;
  $nrow3 = $check_request_2->num_rows;

  $check_request->fetch();
  $check_request_1->fetch();
  $check_request_2->fetch();

  $check_request->close();
  $check_request_1->close();
  $check_request_2->close();

  $check_request->execute();
  $check_request->store_result();
  $check_request->bind_result($reqid, $req_sender, $req_accepter, $req_status);
  $nrow1 = $check_request->num_rows;
  $check_request->fetch();
  $check_request->free_result();
  $check_request->close();

  $check_request_1->execute();
  $check_request_1->store_result();
  $check_request_1->bind_result($reqid1, $req_sender1, $req_accepter1, $req_status1);
  $nrow2 = $check_request_1->num_rows;
  $check_request_1->fetch();
  $check_request_1->free_result();
  $check_request_1->close();

  $check_request_2->execute();
  $check_request_2->store_result();
  $check_request_2->bind_result($reqid2, $req_sender2, $req_accepter2, $req_status2);
  $nrow3 = $check_request_2->num_rows;
  $check_request_2->fetch();
  $check_request_2->free_result();
  $check_request_2->close();