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?

Chris Bier
  • 14,183
  • 17
  • 67
  • 103
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select) – Peter O. Apr 25 '13 at 14:07
  • As it says, array_keys recieves a boolean. See the documentation of reset – Virus721 Apr 25 '13 at 13:48

2 Answers2

0

Check if you get data than only perform any operation. Or just print message No Records Found.

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

Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rikesh
  • 26,156
  • 14
  • 79
  • 87
  • Why would you use `count` and the implicit fact that 0 is equivalent to false when you can use `empty` ? – Virus721 Apr 25 '13 at 13:52
  • Parse error: syntax error, unexpected '{' in H:\Project\xampplite\htdocs\example\phptutorial\doctorav.php on line 15 I have this error when i do that – user2320000 Apr 25 '13 at 14:10
  • I solved actually, there was a un.needed ( that i didn't see in your code thanks! – user2320000 Apr 25 '13 at 14:16
-1

You need to check if the results are empty first before processing them.

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

You probably also need to check this in the output as well, or else the for-each will fail as well :)

2rsvold
  • 17
  • 2
  • If i do this i recieve this error Parse error: syntax error, unexpected '{' in H:\Project\xampplite\htdocs\example\phptutorial\doctorav.php on line 15 – user2320000 Apr 25 '13 at 14:10