2016-06-21 61 views
0

排除空數據庫條目的PHP結果現在停止工作。 我生成了以下測試代碼,它在結果中提供了空值。我很困惑,並不知道如何解決。請幫75歲的我第一次在互聯網問題的網站從上述數據庫空不表示爲空NULL

<body> 
    <?PHP 
    include "dbcfg.php"; 
    $link = mysqli_connect($mysqlserver, $mysqlusername, $mysqlpassword,  $dbname) or 
      die("Error connecting to mysqli server: " . \mysqli_error()); 
    if (!$link) { 
     die("Connection failed: " . mysqli_connect_error()); 
    } 
    $attractquery = "select * from eatingout where town = 'Newcastle'"; 
    $result = \mysqli_query($link, $attractquery) or die 
        ("Query to get data from table failed: " . \mysqli_error()); 
    while ($db_row = mysqli_fetch_array($result)) { // iterate through all selected 
     /*if($db_row['image'] != null){*/ 
     /*if (!empty($db_row['image']));{*/ 
     if(isset($db_row['image']));{ 
     echo $db_row['image'] ." ". $db_row['EOID']."<br/>" ; 
     } 
    } 
     ?> 
</body> 

結果包括許多空白點,然後從任何的三個選項EOID數字。我做了什麼?

+1

你在哪裏得到空,沒有,你使用一個變量,所以我不知道你在哪裏得到那些 –

+0

使用任何地方'如果(!is_null($ db_row ['圖片])) {'...並且在你的if語句中的'{'之前去掉';'。 –

+0

...並且在DB查詢的'WHERE'語句中添加'AND image IS NOT NULL'會更好嗎? –

回答

1

您可以在查詢中添加NULL測試: $ attractquery =「select * from eatout where town ='Newcastle'AND image is NOT NULL」;

+0

這將過濾掉沒有圖像的所有行。從OP代碼我不認爲這是預期的結果,他希望打印所有行,但只顯示可用的圖片。 –

0

請檢查您的列是否包含NULL值,而不是包含「NULL」的字符串。您應該啓用對image列由mysqli_query和mysqli_error之前選中複選框爲NULL

+0

我很驚訝的答覆。嘗試解決方案,如果(!is_null),並刪除我的錯誤分號,但無濟於事。程序本身繼續查詢許多字段以消除空值,因此只需將圖像字段添加到選擇查詢中就不是答案。 – Auldbert

0
<body> 
    <?PHP 
    include "dbcfg.php"; 
    $link = mysqli_connect($mysqlserver, $mysqlusername, $mysqlpassword,  $dbname) or 
      die("Error connecting to mysqli server: " . mysqli_error()); 
    if (!$link) { 
     die("Connection failed: " . mysqli_connect_error()); 
    } 
    $attractquery = "select * from eatingout where town = 'Newcastle' and ifnull(image,'')=''"; 
    $result = mysqli_query($link, $attractquery) or die 
        ("Query to get data from table failed: " . mysqli_error()); 
    while ($db_row = mysqli_fetch_array($result)) { // iterate through all selected 
     /*if($db_row['image'] != null){*/ 
     /*if (!empty($db_row['image']));{*/ 
     if(!is_null($db_row['image'])){ 
     echo $db_row['image'] ." ". $db_row['EOID']."<br/>" ; 
     } 
    } 
     ?> 
</body> 

請刪除「\」有NULL值。

您應該使用!is_null($ db_row ['image'])而不是isset bcoz $ db_row ['image']將始終設置。

其次,您可以檢查它自我查詢空值,如其他答案中所述。

第三有後,如果條款即

if(isset($db_row['image']));{ 
      echo $db_row['image'] ." ". $db_row['EOID']."<br/>" ; 
      } 
1

可能是一個字符串轉換問題colan?

while ($db_row = mysqli_fetch_array($result)) { 
     if(isset($db_row['image']) && $db_row['image'] != NULL && strtolower($db_row['image']) != 'null'){ 
      echo $db_row['image'] ." ". $db_row['EOID']."<br/>" ; 
     } 
    } 
+0

如果原始問題中的'echo'實際上是在屏幕上寫'NULL',這是一個明顯的可能性; PHP的寬鬆輸入意味着應該輸出'NULL ==「」'so ** nothing **「。 – CD001

+0

謝謝你的回答。由bOne提供的解決方案和F3L1X79的實現工作,所以它顯然是一個數據庫字符串問題,即使在PhPmyAdmin中設置了空默認值。也許與通過Bullzip訪問上傳有關。現在我需要將dbase中的所有空字符串轉換爲null – Auldbert