您有 2 行 SELECT,但是它们都有相同的变量:$user_ids 和 $loadmore
错误消息应该显示它警告的内容。但是 $user_ids 看起来声明正确,因此留下了 $loadmore。
它也看起来在function Total_Updates
里面function Updates
?
如果它是一个单独的函数(并且只是一个复制粘贴错误?)您可能只想global $loadmore;
在两个函数的开头使用。
编辑:
可能对代码进行更改以使用全局。有了这个函数Updates
,总是需要先调用Total_Updates
。
public function Updates($user_ids, $lastid)
{
global $loadmore; // Keep available for Total_Updates
if($lastid==0) {
$loadmore = "";
}else{
$loadmore = " AND M.msg_id < $lastid ";
}
$db_conx = mysqli_connect("localhost", "root", "", "test");
$sql = "SELECT M.msg_id, M.uid_fk, M.message, M.created, U.fname, U.lname, M.uploads, M.profile_uid FROM messages M, users U WHERE M.uid_fk=U.uid AND M.uid_fk IN ($user_ids) $loadmore
UNION
SELECT M.msg_id, M.uid_fk, M.message, M.created, U.fname, U.lname, M.uploads, M.profile_uid FROM messages M, users U WHERE M.uid_fk=U.uid AND M.profile_uid IN ($user_ids) $loadmore ORDER BY msg_id DESC LIMIT " .$this->postcount;
$query = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$data[]=$row;
return $data;
}
}
public function Total_Updates($user_ids)
{
global $loadmore; // From Updates
$db_conx = mysqli_connect("localhost", "root", "", "test");
$sql = "SELECT M.msg_id, M.uid_fk, M.message, M.created, U.fname, U.lname, M.uploads, M.profile_uid FROM messages M, users U WHERE M.uid_fk=U.uid AND M.uid_fk IN ($user_ids) $loadmore
UNION
SELECT M.msg_id, M.uid_fk, M.message, M.created, U.fname, U.lname, M.uploads, M.profile_uid FROM messages M, users U WHERE M.uid_fk=U.uid AND M.profile_uid IN ($user_ids) $loadmore ORDER BY msg_id DESC ";
$query = mysqli_query($db_conx, $sql);
$data = mysqli_num_rows($query);
exit(); // This is just for test? it disables return.
return $data;
}
另一种解决方案是为 loadmore ex 创建一个单独的函数:
public function getUpdates_loadmore($lastid)
{
if($lastid==0) return "";
return " AND M.msg_id < $lastid ";
}
为此$loadmore = getUpdates_loadmore($lastid);
,需要将 $lastid 参数添加到function Total_Updates($user_ids)
=>function Total_Updates($user_ids, $lastid)