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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.