0

Possible Duplicate:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in

Ok I have this code.

<?//index.php
if(!defined('INCLUDE_CHECK')) die('You are not allowed to execute this file directly');


include('db.php');//include our database
//connecting to the database
$con = mysql_connect($dbhost, $dbusername, $dbpassword);
//if we cant connect
if (!$con)
{
die ('could not connect to the database' . mysql_error());
}
//if successfully connected then select db
mysql_select_db ($dbtable, $con);
//our query
$result = mysql_query("SELECT * FROM header");
//fetch our result
while($row = mysql_fetch_array($result)) //this is the line 18
{
//if type is meta then..
if ($row['type'] === "meta")
{
$meta .= "<meta name='".$row['name']."' content='".$row['content']."' />";
}
//if type is title then..
elseif ($row['type'] === "title")
{
$title = "<title>".$row['content']."</title>";
$title2 = "<div id='ti'>".$row['name']."</div>";
}
//if type is favicon then..
elseif ($row['type'] === "favicon")
{
$favicon = "<link rel='shortcut icon' href='".$row['content']."' />";
$imglogo = "<img class='imglogo' src='".$row['content']."' />";
}
//if type is description then..
elseif ($row['type'] === "description")
{
$des = "<div id='ti2'>".$row['content']."</div>";
}

}
mysql_close($con);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<? echo $title; //line 50 ?>
<? echo $favicon; //line 51 ?>
<? echo $meta; //line 52 ?>
<? echo (file_get_contents("cc.txt")); ?>
</head>

and I got this following errors, Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\wr\header.php on line 18

Notice: Undefined variable: title in C:\xampp\htdocs\wr\header.php on line 50

Notice: Undefined variable: favicon in C:\xampp\htdocs\wr\header.php on line 51

Notice: Undefined variable: meta in C:\xampp\htdocs\wr\header.php on line 52

the error lines are specified along the code, please see the top code for error lines.

Can some help me into this? thanks in advance.

Juliver.

Community
  • 1
  • 1
Juliver Galleto
  • 8,489
  • 24
  • 76
  • 153

4 Answers4

2

Maybe your SELECT didn't succeed. You should check if $result is FALSE

if ($result === false)

and if so, you can get the error message with mysql_error().

GergelyPolonkai
  • 6,088
  • 5
  • 37
  • 66
1

Before passing the $result into mysql_fetch_array, you should always check that it !== false, because this is the value of it if the query fails, which is what it looks is what happened in your case. See http://php.net/manual/en/function.mysql-query.php. If it did fail you might want to echo mysql_error() to see what went wrong.

Bob Bobbio
  • 587
  • 1
  • 5
  • 10
0

From http://php.net/manual/en/function.mysql-query.php:

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

mysql_query() will also fail and return FALSE if the user does not have permission to access > the table(s) referenced by the query.

Before further processing the result you should always make sure that it is not boolean FALSE (ie. !== FALSE)

scaraveos
  • 986
  • 2
  • 9
  • 12
0

The error persists in one of the below lines so add:

mysql_select_db ($dbtable, $con) OR DIE(mysql_error());

$result = mysql_query("SELECT * FROM header") OR DIE(mysql_error());