首先,我不得不说我喜欢PHP。这太有趣了。所以这是我的问题,我正在处理准备好的陈述和以下工作
<?php
$qm = $con->prepare("SELECT id, receiving_user_id, send_date, message_title, message_body, senders_name FROM Messages WHERE receiving_user_id=? ORDER by id");
/* execute statement */
$qm ->bind_param('i',$userid);
$qm->execute();
/* bind result variables */
$qm->bind_result($messid, $uid, $sent, $title, $body, $dudesname);
/* fetch values */
while ($qm->fetch()) {
echo ($dudesname. $title." ");
}
/* close statement */
$qm->close();
/* close connection */
$sql_e = $con-> prepare("SELECT id FROM Messages WHERE receiving_user_id=?");
$sql_e -> bind_param('i', $userid);
$sql_e -> execute();
$userCheck = $sql_e -> get_result();
$sql_e -> close();
if ($userCheck -> num_rows == 0) {
echo "No Messages... :(";
}
?>
但是,由于$ sql_e语句和$ qm语句完全相同,我认为我可以使用以下内容并且它可以正常工作。但事实并非如此。
$qm = $con->prepare("SELECT id, receiving_user_id, send_date, message_title, message_body, senders_name FROM Messages WHERE receiving_user_id=? ORDER by id");
/* execute statement */
$qm ->bind_param('i',$userid);
$qm->execute();
/* bind result variables */
$qm->bind_result($messid, $uid, $sent, $title, $body, $dudesname);
/* fetch values */
while ($qm->fetch()) {
echo ($dudesname. $title." ");
}
/* close statement */
$qm->close();
/* close connection */
$qm -> execute();
$userCheck = $qm-> get_result();
$qm -> close();
if ($userCheck -> num_rows == 0) {
echo "No Messages... :(";
}
Arent Prepared语句用于输入相同的内容,例如
$qm -> execute();
// Different Variables Here
$qm -> close();
$qm -> execute();
// Yet Another Set of Variables Here
$qm -> close();
很奇怪。对此的任何意见将不胜感激。我是PHP开发的新手,我正在努力学习我能做的一切。
编辑代码以使用$ stmt reset();
$qm = $con->prepare("SELECT id, receiving_user_id, send_date, message_title, message_body, senders_name FROM Messages WHERE receiving_user_id=? ORDER by id");
/* execute statement */
$qm ->bind_param('i',$userid);
$qm->execute();
/* bind result variables */
$qm->bind_result($messid, $uid, $sent, $title, $body, $dudesname);
/* fetch values */
while ($qm->fetch()) {
echo ($dudesname. $title." ");
}
/* reset statement */
$qm->reset();
$qm -> execute();
$userCheck = $qm -> get_result();
if ($userCheck -> num_rows == 0) {
echo "No Messages... :(";
}
/* close statement */
$qm -> close();
答案 0 :(得分:0)
您不需要执行两次查询。我想这就是你想要的:
$qm = $con->prepare("SELECT id, receiving_user_id, send_date, message_title, message_body, senders_name FROM Messages WHERE receiving_user_id=? ORDER by id");
/* execute statement */
$qm ->bind_param('i',$userid);
$qm->execute();
/* bind result variables */
$qm->bind_result($messid, $uid, $sent, $title, $body, $dudesname);
/* fetch values */
$userCheck = $qm->get_result();
if ($userCheck->num_rows > 0) {
while ($userCheck->fetch()) {
echo $dudesname. $title . " ";
}
} else {
echo "No Messages... :(";
}
/* close statement */
$qm -> close();
您当前的代码正在关闭早期的连接。