PHP MySQL WHERE单元格为空白而不返回信息

时间:2012-05-09 14:52:33

标签: php mysql sql

我有一个脚本可以将值插入到数据库中,然后在完成后将标题传递给页面,在该页面中检查某个列中的空白值并将其返回到我稍后可以编辑的页面上,以便可以更新它们

我遇到的问题是当我运行查询时它没有返回带有空值的单元格。

$sql = "SELECT * FROM `data` WHERE `team`=''";

$result = mysql_query($sql, $conn);
if(!$result) {
    echo "Cannot do query" . "<br/>";
    exit;
}

$row = mysql_fetch_row($result);
$count = $row[0];

$each_results = mysql_fetch_row($result);

for ($i=0; $i<$each_results; $i++) {
    $row = mysql_fetch_assoc ($result);
    echo $row['rep_num'] . " " ;
    echo $row['team'] . " ";
    echo $row['description'] . "</br>";
}

我尝试了SELECT * FROM data并返回了所有结果,包括没有团队价值的结果。我在phpmyadmin中运行了SELECT * FROM data WHERE team =''查询,它确实返回了我预期的行以及团队行中缺少的单元格数据。所以我猜我底部的for循环有问题。

先谢谢你的帮助。

2 个答案:

答案 0 :(得分:2)

$each_result不包含您期望的值;您已将结果集中的一行作为数组提取到其中,并且无法进行比较,例如$i < $each_results)而不是for循环,使用while循环迭代行

// Don't call fetch here, as it will advance the record pointer
// before you intend to.
//$row = mysql_fetch_row($result);
//$count = $row[0];

//$each_results = mysql_fetch_row($result);

// Replace the for loop with a while loop
while ($row = mysql_fetch_assoc($result)) {
    echo $row['rep_num'] . " " ;
    // Note, team should be blank because you queried team=''
    echo $row['team'] . " ";
    echo $row['description'] . "</br>";
}

答案 1 :(得分:0)

万一你有NULL和ot空白字段,使用'IS NULL'或'IS NOT NULL'或isnull()mysql函数