1

I've been trying to output my database fields according to their empid, but I somehow can't. it gives me this error..

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:_webhost\Apache24\htdocs\eis\usercp.inc.php on line 16

<?php       
$firstname =  getuserfield('txtFname');
$lastname = getuserfield('txtLname');
echo 'Hello '.$firstname.' '.$lastname.'.';
$empid = getuserfield('empid');
$query = "SELECT type_of_leave,specific_reason,date_from,date_to,num_of_days FROM `hrf_leave` WHERE `empid` = '$empid' AND `formStatus` = 0";
$query_run = mysql_query($query);
echo "<table border=1>
<tr>
<th>Type of Leave</th>
<th>Specific Reason</th>
<th>Date From</th>
<th>Date To</th>
<th>Number of Days</th>
</tr>";
while($record = mysql_fetch_array($query_run)){ // line 16
echo "<tr>";
echo "<td>" . $record['type_of_leave'] . "</td>";
echo "<td>" . $record['specific_reason'] . "</td>";
echo "<td>" . $record['date_from'] . "</td>";
echo "<td>" . $record['date_to'] . "</td>";
echo "<td>" . $record['num_of_days'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>              
juergen d
  • 201,996
  • 37
  • 293
  • 362
Wax
  • 323
  • 2
  • 19
  • put or `die(mysql_error())` after mysql_query; – artragis Feb 06 '13 at 12:22
  • I tried that. I found the problem, I forgot to save mysql table after doing some alteration. But it still doesn't output the results i want. – Wax Feb 06 '13 at 12:43

3 Answers3

1

A boolean false is passed from mysql_query() since your sql statement $query is invalid. Therefor mysql_fetch_array() returns the above error.

Try this as $query, it will solve if it is a syntax error I hope.

   $query= SELECT * FROM hrf_leave WHERE empid = '$empid' AND formStatus = 0";
Vishnu R
  • 1,859
  • 3
  • 26
  • 45
0

See there might be 2 things :-

  1. First the empid you used in the query might be integer and you are passing the value with in quotes.
  2. Second the query is not returning any result. echo out the query and run it in the phpmyadmin or any mysql query browser.

Suggestion : Before using mysql_fetch_array use mysql_num_rows to check that if any rows are returned from the query.

Note : mysql_* functions are being depreciated. So avoid them

Roger
  • 1,693
  • 1
  • 18
  • 34
  • i tried outputting 1 field in the database using $row =mysql_fetch_assoc(query_run) and echoing it like this $row['id'] but nothing popped out – Wax Feb 06 '13 at 12:59
  • @Carnal I know its late but do you still have this problem? – Roger Feb 07 '13 at 05:29
0

Before you do anything please try to not use mysql_ function its no more supported You can use pdo. Bellow is how to connect to the datadabase using pdo from there you have to learn how to query pdo gud luck . getMessage(); } ?>

Humphrey
  • 2,659
  • 3
  • 28
  • 38