gpt4 book ai didi

php - 使用 COUNT 函数计算新消息的数量?

转载 作者:太空宇宙 更新时间:2023-11-03 12:34:19 26 4
gpt4 key购买 nike

我正在尝试从我的表 ptb_messages 中获取用户的新消息数量,其中“已读”设置为“0”

谁能告诉我哪里出错了。

我的表格是这样的:

id | from_user_id | to_user_id | subject | content | date_sent | read

1 2 4 hello hello 2012-04-13 0

我正在使用此函数来尝试显示用户拥有的未读消息数。

function check_new_messages() {
global $connection;
global $_SESSION;
$query = "SELECT COUNT('read') FROM ptb_messages WHERE to_user_id =".$_SESSION['user_id']." AND ('read)'='0'";
$check_new_messages_set = mysql_query($query, $connection);
confirm_query($check_new_messages_set);
return $check_new_messages_set;
}

<?php

$check_new_messages_set = check_new_messages();
while ($new = mysql_fetch_array($check_new_messages_set)) {

echo "There are ". $new['COUNT(read)'] ." items.";

}

?>

最佳答案

你根本不需要COUNT那个字段; WHERE 就足够了。引号只是有点偏离:

$query = "SELECT COUNT(*) FROM ptb_messages WHERE to_user_id =".$_SESSION['user_id']." AND `read` = 0";

此外,请不要使用mysql_ 扩展;它们在 PHP 5.5 中已被弃用,并且很容易引入 SQL 注入(inject)漏洞。尝试使用 PDO 或 MySQLi 准备好的语句!

关于php - 使用 COUNT 函数计算新消息的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13961459/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com