我有一个noob问题,但我没有在网上找到任何答案,因为这是我认为的一些特殊问题。我正在使用自动刷新编写消息系统。
这是为了获取db中的最新ID:
$query = mysql_query("SELECT * FROM `messages`
WHERE `requestor`='".$_POST['user_id']."'
AND `target`='".$_POST['target_id']."'
OR `requestor`='".$_POST['target_id']."'
AND `target`='".$_POST['user_id']."'
ORDER BY `id` ASC");
$rows = mysql_fetch_array($query);
$id = $rows['id']; //latest message id at this moment
$lastSeenID = $_POST['msg_id']; //This is the latest id which is shown on the client
现在我需要ID大于客户端上最后一个已知id的列。 ($ lastSeenID)。
但这不会以正确的方式运作。
mysql_query("SELECT * FROM `messages`
WHERE `id` > '".$lastSeenID."'
AND `requestor`='".$_POST['user_id']."'
AND `target`='".$_POST['target_id']."'
OR `requestor`='".$_POST['target_id']."'
AND `target`='".$_POST['user_id']."'
ORDER BY `id` ASC")
我希望你能帮助我。
答案 0 :(得分:2)
请开始喜欢查询参数:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$city = "Amersfoort";
/* create a prepared statement */
if ($stmt = $mysqli->prepare("SELECT District FROM City WHERE Name=?")) {
/* bind parameters for markers */
$stmt->bind_param("s", $city);
/* execute query */
$stmt->execute();
/* bind result variables */
$stmt->bind_result($district);
/* fetch value */
$stmt->fetch();
printf("%s is in district %s\n", $city, $district);
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
?>
答案 1 :(得分:0)
试试这个:
mysql_query("SELECT * FROM `messages` WHERE (`id` > '$lastSeenID' AND `requestor`='$_POST['user_id']' AND `target`='$_POST['target_id']' OR `requestor`='$_POST['target_id']' AND `target`='$_POST['user_id']') ORDER BY `id` ASC");
或MySQLi
mysqli_query($con,"SELECT * FROM `messages` WHERE (`id` > '$lastSeenID' AND `requestor`='$_POST['user_id']' AND `target`='$_POST['target_id']' OR `requestor`='$_POST['target_id']' AND `target`='$_POST['user_id']') ORDER BY `id` ASC");
答案 2 :(得分:0)
代表OP发表。
我得到了工作代码(感谢括号中的建议)。
mysql_query("SELECT * FROM `messages` WHERE (`id` > '".$lastSeenID."' AND `requestor`='".$_POST['user_id']."' AND `target`='".$_POST['target_id']."') OR (`id` > '".$lastSeenID."' AND `requestor`='".$_POST['target_id']."' AND `target`='".$_POST['user_id']."') ORDER BY `id` ASC");