Forum Moderators: coopster

Message Too Old, No Replies

Resource id #3

mysql query problem?

         

BhatMan

1:07 am on May 17, 2004 (gmt 0)

10+ Year Member



I'm not sure what the problem is, but here's the relevant part of the code:

$result = mysql_query("SELECT * FROM table_1 ORDER BY rec_id DESC LIMIT 0, 20");

after this, when I do
print_r($result);
the output is: "Resource id #3"

and later in the code, when I do this:
$row = mysql_fetch_array($result);

I get the following error in my apache error.log file:
PHP Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in guestbook.php on line 36

I'm not sure what's wrong, so if you have questions about my php configuration, etc, I'll answer them.

thanks in advance

coopster

2:17 am on May 17, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, BhatMan!

The query syntax looks good to me. Are you sure the table name exists and is correct (double check for case sensitivity)? How about the column name? You may also want to make sure that your connection is established and you have selected the correct database.

$link = mysql_connect('server_name', 'userid', 'password') or exit('Could not connect (' . mysql_errno() . '): ' . mysql_error()); 
$database = mysql_select_db('my_database', $link) or exit('Could not select database (' . mysql_errno() . '): ' . mysql_error());

thedagda

2:16 pm on May 17, 2004 (gmt 0)

10+ Year Member



To debug this one, I would call the $row = mysql_.... statement before the print_r statement. See if you get the same error there.

I'm not sure if this is the best way or not, but here is how I code my database pulls in PHP:

$sql = "SELECT * FROM table_1 ORDER BY rec_id DESC LIMIT 0, 20;";

$sql_res = mysql_query($sql); //execute query
//for error checking you can add the following line
print mysql_error();

$row = mysql_fetch_array($sql_res);

I like this way, because if you are passing variables to the sql statement, to see what you passed you just need to echo the $sql variable. Also, by putting the print mysql_error() statment in there, you know right away if you have a problem with the sql (Assuming you have error_reporting() set correctly).

I would try these and see what you come up with.

Conor

[edited by: jatar_k at 5:21 pm (utc) on May 17, 2004]
[edit reason] no sig urls thanks [/edit]

BhatMan

6:47 am on May 18, 2004 (gmt 0)

10+ Year Member



ah, thanks dagda! it worked when I used your code...probably some small syntax error I overlooked.

coopster, you were right too; part of the problem was that I had forgotten to put a value in the url of the script.