简体   繁体   English

MYSQL循环显示额外的“空”结果

[英]MYSQL Loop Showing Extra “Empty” Result

I have deleted the table and started over with new data over and over and tried to figure out why my loop is adding an extra empty result at the beginning. 我已经删除了表,并一遍又一遍地使用新数据,并试图弄清为什么我的循环在开始时添加了一个额外的空结果。 The code that I am using for the loop is below. 我在循环中使用的代码如下。

    $sql = "SELECT * FROM `addresses` WHERE `company_name` = '$pro_company'";
    $query = $mysqli->query("$sql");    
        while($array[] = $query->fetch_object());
            array_pop($array);
            foreach($array as $listing) :
                echo $listing->Taddress . " ";
                echo $listing->Tcity. " ";
                echo $listing->Tstate . " ";
                echo $listing->Tzip . " ";
                echo " <a href='edit.php?pid=". $listing->PID . "'>edit</a> |";
                echo " <a href='delete.php?pid=". $listing->PID . "'>delete</a>";
                echo "</a><br />";
        endforeach;

The results I am getting from this loop are below. 我从此循环中获得的结果如下。

       edit | delete
       14220 Parrott Ext. TestCity AL 84106 edit | delete

I am trying to learn mysqli statements so I am sure there is something I am missing. 我正在尝试学习mysqli语句,因此我确定我缺少一些东西。

Thanks for your help in advance. 感谢您的帮助。

Throw a 扔一个

print_r($array);die;

Before the foreach and look at the output. 在foreach之前,先看一下输出。 This should give you an idea of where exactly the problem is located. 这应该使您知道问题的确切位置。

Most likely your database has a row with no values. 您的数据库很可能有一行没有值的行。 Check that first. 首先检查。 happens more often than you realise (to me anyway) 发生的频率超出了您的预期(无论如何对我来说)

As a possible solution, you can add an extra condition before echo'ing to ensure a value exists 作为一种可能的解决方案,您可以在回显之前添加一个附加条件以确保存在一个值

foreach($array as $listing) :
   if($listing->Taddress){
     echo $listing->Taddress . " ";

Personally, I'd find the cause first. 就个人而言,我会首先找到原因。 print_r will help print_r会有所帮助

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

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