2

我想从 to 切换到mysql_*mysqli_*因为我已经阅读,很快mysql_*将不再支持。

现在我有一个问题,我写的函数不再起作用了。

在 index.php 文件中我有

global $connect;
$connect = mysqli_connect('localhost', "username", "password");

在functions.php文件中我有

function count_total_messages(){
        $result = "SELECT COUNT(id) AS total FROM database.messages WHERE uidto='".$_SESSION['userid']."' OR unameto='".$_SESSION['username']."'";
        $qry = mysqli_query($connect,$result);
        return $qry['total'];
    }

但我收到以下错误:

未定义变量:连接

我应该怎么办?mysqli_query除了functions.php文件中的那些之外,到处都是作品。

4

2 回答 2

5
function count_total_messages(){
        global $connect;  //You need a locally defined connect variable
        $result = "SELECT COUNT(id) AS total FROM database.messages WHERE uidto='".$_SESSION['userid']."' OR unameto='".$_SESSION['username']."'";
        $qry = mysqli_query($connect,$result);
        return $qry['total'];
    }
于 2013-10-31T23:35:38.770 回答
1

global做错了。在您的索引中,您只需要

$connect = mysqli_connect('localhost', "username", "password");

在你的函数中你使用这个:

function count_total_messages(){
    global $connect;
    $result = "SELECT COUNT(id) AS total FROM database.messages WHERE uidto='".$_SESSION['userid']."' OR unameto='".$_SESSION['username']."'";
    $qry = mysqli_query($connect,$result);
    return $qry['total'];
}
于 2013-10-31T23:35:20.753 回答