繁体   English   中英

PHP,如果其他时(与MySQL)语句不起作用

[英]PHP if else while (with mysql) statement not working

我在我的函数中有这段代码,如果它无法获取存储在变量$ result中的数据,则会回显“ Empty”。 “空”部分工作正常。 但是,如果条件进入else部分,它将不会回显用户数据,为什么?

$result = mysql_query("SELECT * FROM `users` ORDER BY `earnings` DESC LIMIT ".$load.",1") or die (mysql_error());
if(!mysql_fetch_row($result)) {
    echo "Empty";
} else {
    while($data = mysql_fetch_array($result))
    {
        if($name == true) {
            echo ucfirst($data['user']);
        } else {
            echo ucfirst($data['earnings']);
        }
    }
}

通过调用mysql_fetch_row($result)作为测试,您总是会丢失数据的第一行。 检查行数是否为0。

您好,我认为您的问题在mysql_fetch_array()中。 此函数将返回数组而不是关联数组。

您可以使用

$result = mysql_query("SELECT * FROM `users` ORDER BY `earnings` DESC LIMIT    ".$load.",1") or die (mysql_error());
if(mysql_num_rows($result) == 0) {
   echo "Empty";
} else {
     while($data = mysql_fetch_array($result,MYSQL_BOTH))
 {
    if($name == true) {
        echo ucfirst($data['user']);
    } else {
        echo ucfirst($data['earnings']);
    }
 }
}

使用mysql_fetch_row的mysql_num_rows intead检查空记录集。

我在mysql_fetch_array的MYSQL_BOTH中传递第二个参数,该参数将同时返回数组和关联数组。

或者你可以使用

mysql_fetch_assoc()函数而不是mysql_fetch_array()。

随时提出问题。

尝试这个

$result = mysql_query("SELECT * FROM `users` ORDER BY `earnings` DESC LIMIT ".$load.",1") or die (mysql_error());
    if(!$result) {
        echo "Empty";
    } else {
        while($data = mysql_fetch_array($result))
        {
            if($name == true) {
                echo ucfirst($data['user']);
            } else {
                echo ucfirst($data['earnings']);
            }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM