haditedi Posted January 18, 2011 Share Posted January 18, 2011 Hi All, pls help me with this I have include a 'connect.php' but it resulted error "mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\carpark\viewcars.php on line 21" but if I dont use php include then everything works fine,,, below the code : <?php include "connect.php"; echo "<table border='1' id='position'> <tr> <th>Ticket</th> <th>Car Name</th> <th>Car Out</th> </tr>"; $result=mysql_query("select * from car"); while ($row=mysql_fetch_array($result)) { echo "<tr>"; echo "<td>".$row['ticket']."</td>"; echo "<td>".$row['carname']."</td>"; echo "</tr>"; } echo "</table>"; mysql_close(); ?> connect.php <? $host="localhost"; $user="root"; $password=""; mysql_connect($host,$user,$password); mysql_select_db("car"); ?> If I use below ,,everything works fine ... what went wrong ??? <? $host="localhost"; $user="root"; $password=""; mysql_connect($host,$user,$password); mysql_select_db("car"); echo "<table border='1' id='position'> <tr> <th>Ticket</th> <th>Car Name</th> <th>Car Out</th> </tr>"; $result=mysql_query("select * from car"); while ($row=mysql_fetch_array($result)) { echo "<tr>"; echo "<td>".$row['ticket']."</td>"; echo "<td>".$row['carname']."</td>"; echo "</tr>"; } echo "</table>"; mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/224820-php-include-and-mysql/ Share on other sites More sharing options...
trq Posted January 18, 2011 Share Posted January 18, 2011 You should always check your query succeeds and returns a result before attempting to use it. if ($result=mysql_query("select * from car")) { if (mysql_num_rows($result)) { while ($row=mysql_fetch_array($result)) { echo "<tr>"; echo "<td>".$row['ticket']."</td>"; echo "<td>".$row['carname']."</td>"; echo "</tr>"; } } else { // no results found. } } else { // query failed, handle error } Quote Link to comment https://forums.phpfreaks.com/topic/224820-php-include-and-mysql/#findComment-1161254 Share on other sites More sharing options...
haditedi Posted January 18, 2011 Author Share Posted January 18, 2011 Thx for the reply Thorpe but still wont work,,, before my error was "mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\carpark\viewcars.php on line 21" now it changed to mysql_num_rows() expects parameter 1 to be resource, boolean,,,,, Quote Link to comment https://forums.phpfreaks.com/topic/224820-php-include-and-mysql/#findComment-1161318 Share on other sites More sharing options...
Pikachu2000 Posted January 18, 2011 Share Posted January 18, 2011 When posting code, please enclose it within the forum's . . . BBCode tags. Now that that's out of the way, are you sure you're connecting to and selecting the database and successfully? Quote Link to comment https://forums.phpfreaks.com/topic/224820-php-include-and-mysql/#findComment-1161353 Share on other sites More sharing options...
haditedi Posted January 18, 2011 Author Share Posted January 18, 2011 After all day ,,,wondering and pondering ,,, finally the good God shows the way ,,, I tried the code from <? to <?php and it works nowww!!! but still dont know the reason behind this though,,,,, Quote Link to comment https://forums.phpfreaks.com/topic/224820-php-include-and-mysql/#findComment-1161528 Share on other sites More sharing options...
Pikachu2000 Posted January 18, 2011 Share Posted January 18, 2011 Short open tags aren't enabled by default, so you should make it a habit to always use the full <?php tag syntax. Quote Link to comment https://forums.phpfreaks.com/topic/224820-php-include-and-mysql/#findComment-1161532 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.