doulouli8686 2012-05-09 14:52
浏览 93
已采纳

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

I have a script to insert values into a database and then once done passes a header to a page where it checks for blank values in a certain column and returns them on the page which I will later make editable so they can be updated.

The problem I'm having is when I'm running the query its not returning the cells with the blank values.

$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>";
}

I tried SELECT * FROM data and it returned all the results including the results with no team value. I ran the SELECT * FROM data WHERE team='' query in phpmyadmin and it did return the rows I expected it to with the missing cell data from the team row. So I guess its something wrong with my for loop at the bottom.

Thank you in advanced for your help.

  • 写回答

2条回答 默认 最新

  • duandeng7132 2012-05-09 14:54
    关注

    $each_result does not contain the value you expect it to; you have fetched a row from the result set into it as an array, and cannot do a comparison like $i < $each_results) Rather than a for loop, iterate your rows with a while loop

    // 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条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献