比较mySQL值是错误的

时间:2017-06-11 01:47:37

标签: php mysql database

我正在尝试比较mySQL数据库值以查看它是1还是0.我可以连接到数据库并回显整个表和列“运行”就好了:

if($is_query_run = mysql_query($query)){
    echo "Successfully running query on database <br>";
    while($query_execute = mysql_fetch_assoc($is_query_run)){
        echo $query_execute['Run'];
    }
}

列“运行”和行ID“8”的mySQL表值为:1

但下面的代码输出:

  

结果:资源ID#5

     

数据库值不是1或0,因此出现了问题

$number = 8;
$result = mysql_query("SELECT Run FROM users WHERE id='$number'");

echo 'Result: '.$result.'<br><br>';

if($result < 1) {
    echo "Database value is 0 and thus NOT updated today";
} elseif($result == 1) {
    echo "Database value is 1 and thus HAS been updated today";
} else {
    echo "Database value is NOT 1 or 0 and thus something broke";
}

我只是太困惑了。

1 个答案:

答案 0 :(得分:0)

您使用的MySQL扩展在PHP 5.5.0中已弃用,并且在PHP 7.0.0中也被删除。相反,应该使用MySQLi或PDO_MySQL扩展。

你不能只回显$ result。您必须在结果集上检索行或循环才能回显。这就是你获得资源的原因。

请试试这个:

$number = 8;
$result = mysql_query("SELECT Run FROM users WHERE id='$number'");
$row = mysql_fetch_assoc($result);
echo 'Result: '.$row['column_goes_here'].'<br><br>';

if($row['column_goes_here'] < 1) {
    echo "Database value is 0 and thus NOT updated today";
} elseif($row['column_goes_here'] == 1) {
    echo "Database value is 1 and thus HAS been updated today";
} else {
    echo "Database value is NOT 1 or 0 and thus something broke";
}