Jump to content

php include and mysql


haditedi

Recommended Posts

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();

?>

 

 

Link to comment
Share on other sites

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
}

 

Link to comment
Share on other sites

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,,,,,

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.