Abnormal Behaviour with MySql Query in PHP

Go To StackoverFlow.com

-1

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 am using latest version of XAMPP (i.e 1.7.7)
  • I am using PHP 5.3.8.
  • Upgraded the phpmyadmin version to 3.4.10.2 few days ago.
  • rest are default XAMPP 1.7.7 Settings.

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;
    }

}
2012-04-04 16:37
by Abhinav Kulshreshtha
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
The code is deprecated. So it should show a deprecation warning. But here the code is returning Boolean in $result. It should return a resource - Abhinav Kulshreshtha 2012-04-04 20:08
what's your point? It's still deprecated. If you don't want problems, then don't use it. It even says in the docs: Relying on this feature is highly discouraged.Flukey 2012-04-04 20:23
I have changed the code according to the first answer below. 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
WHy can't you show your whole code - Flukey 2012-04-04 20:36
Edited the complete code. Hope now it can help to detect my error - Abhinav Kulshreshtha 2012-04-04 20:50
you're not opening a connection. do you have error_reporting enabled - Flukey 2012-04-04 21:33
** require_once ('./classes/mysql_connect.php');**
The Connection is opened here. I am not that stupid.
`

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



2

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.

2012-04-04 16:41
by freeone3000
Changed code as per your recommendations, now it is not entering the while loop.
This is the complete error 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
i have confirmed that $result is a resource by print_r($result);.
the outcome was Resource id #6Abhinav Kulshreshtha 2012-04-04 20:27
New code, new issues. "public function RSS()" should be "public function __construct()". Second, do your lines in your code match up exactly with lines in RSS.class.php? The error seems to be saying that $details is undefined, which is obviously false. What line of the paste matches up with the line of the error - freeone3000 2012-04-04 23: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

Okay, so your errors weren't from the posted code. The while loop isn't entered if $row is false, which is if mysqlfetchassoc() has no (more) rows, as described in http://php.net/mysqlfetchassoc . So, your query returns no rows - freeone3000 2012-04-05 19:21
Thanks... Didn't play with the return type. I will be more careful from now on - Abhinav Kulshreshtha 2012-04-06 20:28


2

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

2012-04-04 16:53
by Your Common Sense
No error is triggered till your code. But now the control is not going inside the while loop.

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

So define details... you've only defined detail.. - Flukey 2012-04-04 20:37
the variable is $details. now i have pre declared it outside the while loop, but since while loop is the part which generate the most of the XML, its execution is importan - Abhinav Kulshreshtha 2012-04-04 20:53


1

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());
2012-04-04 16:42
by Ibu
haha, the mysql extension is being deprecated, he should start using the mysqli functions or pdo_mysql functions - Flukey 2012-04-04 16:46
Even mysql_query() gets depricated.. You should use mysqli( - suiz 2013-05-22 05:56
Ads