-4

您好,我有一个错误脚本,我不知道为什么,但定义了变量:

 public function Updates($user_ids,$lastid) {
    if($lastid==0)  {
        $loadmore = "";     
    }else{
        $loadmore = " AND M.msg_id < $lastid ";
    }

 public function Total_Updates($user_ids) 

{

$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();

    return $data;

}

未定义的变量与 SELECT 位于同一行。我正在等待您的帮助...谢谢

4

2 回答 2

0

事实上,我的脚本看起来和它一模一样:

public function Updates($user_ids,$lastid) 
{
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) 

{

        $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();
    return $data;       
}

在第一个 $sql 中,变量 $loadmore 运行正确,但在第二个 $sql 中它不起作用。我总是有同样的错误:

Notice: Undefined variable

于 2013-08-26T10:47:45.457 回答
0

您有 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)

于 2013-08-26T00:21:48.220 回答