Page 1 of 1

mysql_fetch_array() error: invalid MySQL result resource?

Posted: Wed Jun 18, 2008 1:34 am
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.

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

Posted: Wed Jun 18, 2008 4:46 am
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

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

Posted: Wed Jun 18, 2008 5:58 am
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());

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

Posted: Wed Jun 18, 2008 6:13 am
by ragnis12
are you sure, you have connected to the database?

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

Posted: Wed Jun 18, 2008 2:14 pm
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.

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

Posted: Wed Jun 18, 2008 4:10 pm
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