从PHP 5.6迁移到7.0后,mysqli_num_rows无法正常工作

时间:2019-07-23 10:26:22

标签: php mysqli

我最近将网站从PHP 5.6更新到7.0。 mysqli_num_rows函数生成一个空结果。这与Count number of rows in Mysql using PHP7类似(在这种情况下,数据库连接设置不正确)。

我根据PHP 7.0更改了脚本。数据库连接有效。该查询似乎有效(没有错误),但没有mysqli_num_rows函数。

数据库连接(common.php):

function db_connect() {
    global $dbhost, $dbuser, $dbpassword, $default_db;

    $link = mysqli_connect($dbhost,$dbuser,$dbpassword);
    if (!$link)
    {
        die("Connection failed to $dbhost.");
    }
    if (!@mysqli_select_db($link,$default_db))
    {
        die("Couldn't select database $default_db.");
    } 

    return $link;
}

查询在PHP 5.6中工作

require("common.php");
db_connect();
$query = "SELECT titel, upc FROM $alben WHERE shop = 1 ORDER BY id DESC";
$result = mysql_query($query);
$i = 0;
if (mysql_num_rows($result) == 0) {
    echo "No results!";
} else {
    while ($album = mysql_fetch_array($result)) {
    $i++;
    some HTML...
}

查询不适用于PHP 7.0

require("common.php");
db_connect();
$query = "SELECT titel, upc FROM $alben WHERE shop = 1 ORDER BY id DESC";
$result = mysqli_query($link, $query);
$i = 0;
if (mysqli_num_rows($result) == 0) {
    echo "No results!";
} else {
    while ($album = mysqli_fetch_array($result)) {
    $i++;
    some HTML...
}

我得到“没有结果!”信息。问题似乎出在mysqli_num_rows函数上。有什么想法吗?

我解决了这个问题。问题是在函数db_connect()中声明了数据库连接。我删除了,现在一切正常。这是更新的代码:

数据库连接(common.php):

    $link = mysqli_connect($dbhost,$dbuser,$dbpassword);
    if (!$link)
    {
        die("Connection failed to $dbhost.");
    }
    if (!@mysqli_select_db($link,$default_db))
    {
        die("Couldn't select database $default_db.");
    } 

查询:

require("common.php");
$query = "SELECT titel, upc FROM $alben WHERE shop = 1 ORDER BY id DESC";
$result = mysqli_query($link, $query);
$i = 0;
if (mysqli_num_rows($result) == 0) {
    echo "No results!";
} else {
    while ($album = mysqli_fetch_array($result)) {
    $i++;
    some HTML...
}

0 个答案:

没有答案