Possible Duplicate:
Warning: mysql_fetch_* expects parameter 1 to be resource, boolean given error
I am doing a project in PHP with MySQL as the database.
This is my code snippet.
$query = "SELECT * FROM FEED_DETAILS";
$result = mysql_db_query (DB_NAME, $query, $conn);
while($row = **mysql_fetch_assoc($result)**)
{
// Things i need to execute....
}
The DB_NAME
is defined by me earlier.
This used to work for me before. But this time it is giving the following error
<b>Warning</b>: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in <b>C:\xampp\htdocs\My_Rss\classes\RSS.class.php</b> on line <b>28</b><br />
The only parameter i am using is $result. I have checked php.net
website for the docs related to database query in php. i have even tried the basic mysql_query
and mysql_fetch_array
.
Same error is generated in all the previous programs i made when i was learning php last year. They all used to work before. This time they have stopped working.
N.B.
I have heard that php 5.3 have major changes and are causing problems. Is this the problem, cause i cant pin point the error in the code..
This is index.php
<?
header("Content-Type: application/xml; charset=ISO-8859-1");
include("classes/RSS.class.php");
$rss = new RSS();
echo $rss->GetFeed();
?>
and this is RSS.class.php
class RSS
{
public function __construct()
{
require_once ('./classes/mysql_connect.php');
}
public function GetFeed()
{
return $this->getDetails() . $this->getItems();
}
private function dbConnect()
{
//DEFINE ('LINK', mysql_connect (DB_HOST, DB_USER, DB_PASSWORD));
}
private function getDetails()
{
$detailsTable = "webref_rss_details";
$this->dbConnect($detailsTable);
$query = "SELECT * FROM FEED_DETAILS";
$result = mysql_query ($query) or trigger_error(mysql_error()." ".$query);
while($row = mysql_fetch_assoc($result))
{
$details = '<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">
<channel>
<title>'. $row['title'] .'</title>
<link>'. $row['link'] .'</link>
<description>'. $row['description'] .'</description>
<language>'. $row['language'] .'</language>
<image>
<title>'. $row['image_title'] .'</title>
<url>'. $row['image_url'] .'</url>
<link>'. $row['image_link'] .'</link>
<width>'. $row['image_width'] .'</width>
<height>'. $row['image_height'] .'</height>
</image>';
}
return $details;
}
private function getItems()
{
$itemsTable = "webref_rss_items";
$this->dbConnect($itemsTable);
$query = "SELECT * FROM FEED_ITEMS";
$result = mysql_query ($query) or trigger_error(mysql_error()." ".$query);
//print_r($result);
//$row = mysql_fetch_assoc($result);
//print_r($row);
$items = '';
while($row = mysql_fetch_array($result))
{
$items .= '<item>
<title>'. $row["title"] .'</title>
<link>'. $row["link"] .'</link>
<description><![CDATA['. $row["description"] .']]></description>
</item>';
}
$items .= '</channel>
</rss>';
return $items;
}
}
Relying on this feature is highly discouraged.
Flukey 2012-04-04 20:23
print_r($result);
is showing that Resource id #6
. But the while loop is still not getting the control. I have added in comments, the changes, look at my comment in first answe - Abhinav Kulshreshtha 2012-04-04 20:32
** require_once ('./classes/mysql_connect.php');**
DEFINE ('DBUSER', 'root'); DEFINE ('DBPASSWORD', 'Qwsa1234'); DEFINE ('DBHOST', 'localhost'); DEFINE ('DBNAME', 'rssfeed');
// Make the connnection and then select the database. $dbc = mysqlconnect (DBHOST, DBUSER, DBPASSWORD) OR die ('Could not connect to MySQL: ' . mysqlerror() ); mysqlselectdb (DBNAME) OR die ('Could not select the database: ' . mysql_error() );
?> - Abhinav Kulshreshtha 2012-04-05 07:03
mysql_db_query() returns FALSE on failure. First, always use mysql_select_db() and mysql_query() over mysql_db_query(). Second, check your result code for errors before using it. mysql_error() can retrieve the error for debugging purposes.
XML Parsing Error: junk after document element
Location: http://localhost/My_rss/
Line Number 2, Column 1:<b>Notice</b>: Undefined variable: details in <b>C:\xampp\htdocs\My_Rss\classes\RSS.class.php</b> on line <b>47</b><br />
^
Abhinav Kulshreshtha 2012-04-04 20:25
print_r($result);
. Resource id #6
Abhinav Kulshreshtha 2012-04-04 20:27
$details = '';
, just before the while loop, was added by me in aftermath of undefined variable error. But actually the $details
inside the while loop is the real deal needed. I have Edited the code once again as per you suggested. results are same as previous.
I cant figure out why the control in not entering the while loop.
I am Building RSS FEED project. and major part of xml is defined inside the while loop. So it is importan - Abhinav Kulshreshtha 2012-04-05 07:18
It is not mysql_fetch_assoc() but mysql_db_query problem.
Run your queries [at least] this way
$query = "SELECT * FROM FEED_DETAILS";
$result = mysql_query ($query) or trigger_error(mysql_error()." ".$query);
so, you'll always be notified of any problem. or, better, encapsulate this code into some abstraction library.
it is also helps to always have error_reporting
at E_ALL
level and have display_errors
setting on
in the development environment
print($result)
shows Resource id #6
but execution stops there.
Then i tried print_r($row);
to see why it is not entering the loop. no luck with that either. I am declaring a variable $detail
inside while, the final error is XML Parsing Error: junk after document element
Location: http://localhost/My_rss/
Line Number 2, Column 1:<b>Notice</b>: Undefined variable: details in <b>C:\xampp\htdocs\My_Rss\classes\RSS.class.php</b> on line <b>47</b><br />
^
Abhinav Kulshreshtha 2012-04-04 20:24
mysql_db_query()
is deprecated as of php 5.3
use mysql_query()
Example:
mysql_select_db(DB_NAME);
$result = mysql_query ($query, $conn) or die(mysql_error());
mysql_db_query
- This function has been DEPRECATED as of PHP 5.3.0. It's not abnormal behaviour, your code is wrong - Flukey 2012-04-04 16:42