mysql_fetch_array() error: invalid MySQL result resource?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zachrose
Forum Newbie
Posts: 5
Joined: Mon Jun 16, 2008 12:34 pm

mysql_fetch_array() error: invalid MySQL result resource?

Post by zachrose »

Hi all,

I'm stumped on what seems like should be just a simple retrieval of one cell in my mysql table. I'v read the manual for mysql_fetch_array() and I don't see what the problem is. The MySQL query works fine as in my MySQL client.

Code: Select all

<?php
    require_once ('inc/dbopen.php');
?>
            <div id="messages">
                <textarea name="message" cols="80" rows="7">
        <?php
 
                $result = mysql_query("SELECT message FROM content ORDER BY created DESC LIMIT 1;");
                $row = mysql_fetch_array($result);  //This is row 20
                echo $row[0];
        ?>
                </textarea>
            </div>
 
returns:
<br />
<b>Warning</b>: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/Applications/MAMP/htdocs/diversified/index.php</b> on line <b>20</b><br />
inside my text area.

Any thoughts?
Thank you.
User avatar
highjo
Forum Contributor
Posts: 118
Joined: Tue Oct 24, 2006 1:07 pm

Re: mysql_fetch_array() error: invalid MySQL result resource?

Post by highjo »

have you test you query in a mysql console? i mean what do you see when you execute the query derectly in mysql?

According to you query, the result set can be a lot of row to fetch.If so you can't display all the rows at once.You have to loop inside the result set.it's usually done with while loop because you don't really know exactly how many rows you would have.
try to query in mysql to find out.Then you would understand better :D

Ps: you can do the same if you know that exactly one query would match
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: mysql_fetch_array() error: invalid MySQL result resource?

Post by WebbieDave »

You can check what's wrong with your SQL by temporarily changing line 8 to:

Code: Select all

$result = mysql_query("SELECT message FROM content ORDER BY created DESC LIMIT 1;") or die(mysql_error());
ragnis12
Forum Newbie
Posts: 12
Joined: Tue Jun 17, 2008 4:14 am

Re: mysql_fetch_array() error: invalid MySQL result resource?

Post by ragnis12 »

are you sure, you have connected to the database?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: mysql_fetch_array() error: invalid MySQL result resource?

Post by califdon »

zachrose wrote:<b>Warning</b>: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in <b>/Applications/MAMP/htdocs/diversified/index.php</b> on line <b>20</b><br />
So it is telling you that mysql_fetch_array() can't work because its argument, $result, doesn't contain any results from your mysql_query().

Since the query is so simple, the almost certain reason is that it never connected to the database.

As WebbieDave showed you, always use error trapping so that you can see what the problem is, if the query fails.

And of course we can't tell you why it never connected to the database because you didn't show us the code that is supposed to connect to the database.
huma
Forum Newbie
Posts: 2
Joined: Wed Jun 18, 2008 3:34 pm

Re: mysql_fetch_array() error: invalid MySQL result resource?

Post by huma »

highjo wrote:have you test you query in a mysql console? i mean what do you see when you execute the query derectly in mysql?

According to you query, the result set can be a lot of row to fetch.If so you can't display all the rows at once.You have to loop inside the result set.it's usually done with while loop because you don't really know exactly how many rows you would have.
try to query in mysql to find out.Then you would understand better :D

Ps: you can do the same if you know that exactly one query would match
================================
highjo's method was the simplest and the fastest way for you problem
Post Reply