2016-06-13 60 views
1

我有一個名爲booktable的表,如截圖所示。當我運行查詢輸出是Table availabe雖然它應該是Table not available.
最初,我試圖檢查條件,而不使用array_key_exists和解決方案在下面提到,但沒有一個工作。MySQL服務器沒有返回正確的輸出

<?php 
require 'dbconnect.php'; //connection to db is success 

$search = "SELECT Table_list FROM booktable WHERE status='booked'"; 
$table_query = mysqli_query($dbconnect,$search); 
$row_check = mysqli_num_rows($table_query); 

if($row_check>=1){ 
    $tables = array(); 
    while($row=mysqli_fetch_assoc($table_query)){ 
     $output = $row['Table_list'].'<br>'; 
     echo $tables[] = $output; //returns o/p: Table1, Table2, Table3 
     } 
    if(array_key_exists('Table1',$tables)){ //trying to check if 'Table1' is in the array 
     echo 'Table not available'; //if 'Table1' exists should echo this line 
    }else{ 
     echo'Table available'; 
    } 
} 
?> 

table:booktable

如果我把array_key_exists部分內while loop不把循環內的說法,即有在O/P沒有變化的O/P是相同的。
所以,我試圖採取這種方法,但我沒有像以前那樣得到正確的o/p。這種方法有什麼問題?

+1

嘗試像,如果($輸出== '表1') –

+0

@ Kaushal國王仍然得到同樣的O/P,沒有變化。 – 007mrviper

+0

你已經在查詢'status ='booked'',這意味着它返回的所有表格都不可用,請詳細解釋你想要達到的目標。 –

回答

1

$輸出變量覆蓋與最後一排值的最後一個值是表3是=表1

原因: while循環結束之前if語句和最後一個值存儲在$輸出變量

代碼:

if ($row_check >= 1) 
{ 
     while ($row = mysqli_fetch_assoc($table_query)) 
     { 
      $output = $row['Table_list'] ; 

      if ($output == 'Table1') 
      { 
        echo $output .' Table not available <br>'; 
      } 
      else 
      { 
        echo $output .' Table available <br>'; 
      } 
     } 
} 
+0

我知道爲什麼我得到錯誤的結果,但我做了你提到的,但我仍然沒有得到正確的O/P。即使'$ output =='Table1''其中'$ output = Table1',我得到'Table available',但它應該是'Table not available'。你能否解釋爲什麼會發生這種情況? – 007mrviper

+0

@ 007mrviper可能是空格問題在if語句之前使用trim或var_dump($ output)並檢查它是什麼返回 –

+0

我試過用if(trim($ output)=='Table1')'&'var_dump($ output )=='表1',但o/p沒有變化。另外,我手動檢查分貝,但沒有空格。 – 007mrviper

1

這是你想要做什麼錯誤的做法:

while($row=mysqli_fetch_assoc($table_query)){ 
     echo $output = $row['Table_list'].'<br>'; 
    } 
    if($output==('Table1')){ 
     echo'Table not available'; 
    }else{ 
     echo'Table available'; 
    } 

因爲用while你運行所有的表,只有你試着檢查。並以非常奇怪的方式。在你的情況,如果我理解正確的話,完整的句子應該是這樣的:

while($row=mysqli_fetch_assoc($table_query)){ 
    $output = $row['Table_list']; 
    echo $output.'<br>'.'Table not available'; 
} 

這將打印這樣的事情:

Table1 
Table not available 
Table2 
Table not available 
Table3 
Table not available 

僅供Table1它應該是這樣的:

while($row=mysqli_fetch_assoc($table_query)){ 
    $output = $row['Table_list']; 
    if($output == 'Table1'){ 
     echo $output.'<br>'.'Table not available'; 
    } 
} 
+0

我想檢查'Table1'是否可用。我不想顯示其他表格的信息。那麼,我怎樣才能以正確的方式實現結果呢? – 007mrviper

+0

@ 007mrviper我有更新我的答案。只是檢查它是否是'Table1',但我建議修改你的select只接受關於'Table1'的信息,如果它只是你需要的表格 – Gvidas

+0

我編輯了我的代碼並且像你說的那樣做了它,但它沒有顯示任何結果。如果我添加一個'else'語句,那麼它顯示'else'語句的信息,但是'if'語句的結果不顯示。與之前遇到的問題相同。 – 007mrviper

0

如果要保留代碼,請使用in_array()而不是array_key_exists()

if(in_array('Table1',$tables)) 
0
$tables = array(); 
while($row=mysqli_fetch_assoc($table_query)){ 
    $tables[] = $row['Table_list'].'<br>';//value stored: Table1, Table2, Table3 
} 

print_r($tables) //displays the stored data on '$tables', this line is optional 
$a = print_r($tables[0]); 
$b = print_r($tables[1]); 
$c = print_r($tables[2]); 

$tables1 = array($a,$b,$c); 

if(in_array('Table1',$tables1)){ 
    echo 'Table not available'; 
}else{ 
    echo'Table available'; 
}