0

This is the code i'm talking about

session_start();

include ('configg.php');

$query = "SELECT Date, Time FROM gp_appointment WHERE Name = '$_POST[doctor]' AND Date = '$_POST[date]'";

$result = mysql_query($query);


  $data = array();

  while($row = mysql_fetch_assoc($result))
  {
     $data[] = $row;

  }

 $colNames = array_keys(reset($data))


 ?>

 <table border="1">
 <tr>
    <?php

       //print the header
       foreach((array)$colNames as $colName)
       {
          echo "<th>$colName</th>";
       }

    ?>
 </tr>

    <?php

       //print the rows
       foreach((array)$data as $row)
       {
          echo "<tr>";
          foreach($colNames as $colName)
          {
             echo "<td>".$row[$colName]."</td>";
          }
          echo "</tr>";
       }


    ?>
 </table>
 <a href="homepage.php">Go back to the homepage</a><br>
 <a href="docname.php">Check another doctor</a><br>

When i chose a date that exist in the databse all works fine, but if a choose a date not-existent i recieve this error:

Warning: array_keys() expects parameter 1 to be array, boolean given in H:\Project\xampplite\htdocs\example\phptutorial\doctorav.php on line 15

Somebody know what to do?

4

2 回答 2

0

检查您是否获取数据而不是仅执行任何操作。或者只是打印消息No Records Found

if((!empty($data)){
     // Perform all operation
}
else{
   echo "No records Found";
}

注意: 请不要mysql_*在新代码中使用函数。它们不再被维护并被正式弃用。看到红框了吗?改为了解准备好的语句,并使用PDOMySQLi -本文将帮助您决定使用哪个。如果您选择 PDO,这里有一个很好的教程

于 2013-04-25T13:49:45.183 回答
-1

在处理它们之前,您需要先检查结果是否为空。

if($data){
    $colNames = array_keys(reset($data));
}

您可能还需要在输出中检查这一点,否则 for-each 也会失败:)

于 2013-04-25T13:48:25.130 回答