2016-06-13 72 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'; 
}