我有一个脚本将值插入数据库,然后在完成后将标题传递到页面,在该页面中检查某个列中的空白值并将它们返回到页面上,稍后我将使其可编辑以便可以更新。
我遇到的问题是当我运行查询时,它不返回带有空白值的单元格。
$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>";
}
我尝试从data
中选择 * 它返回了所有结果,包括没有团队值的结果。我在phpmyadmin中运行了SELECT * FROM data
WHERE team
='' 查询,它确实返回了我期望的行以及团队行中缺少的单元格数据。所以我想底部的 for 循环有问题。
提前感谢您的帮助。
$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>";
}
以防万一你有空和 ot 空白字段,请使用 'IS NULL' 或 'IS NOT NULL' 或 isnull() mysql 函数