1

Encountered with the following warning when trying to access the page:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/designand/www/www/random.php on line 13

Everything was working fine when I was testing it on XAMPP.

<?php
$db_hostname = "localhost";
$db_username = "root";
$db_name = "links";
$db_pass = "xxx";


$dbh = mysql_connect ($db_hostname, $db_username, $db_pass) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db($db_name) or die(mysql_error());

$num_displayed = 1 ;
$result = mysql_query ("SELECT * FROM 'links' ORDER BY RAND() LIMIT $num_displayed"); 
while($row = mysql_fetch_array( $result )) 

{
    echo "<a href=\"" . $row["link"] . "\"><img src=\"" . $row["image"] . "\" border=0 alt=\"\"></a>" ;
}
mysql_close($dbh);
?>
5
  • what differences are there between your setup on XAMPP and your setup on the server? Commented Feb 9, 2011 at 1:02
  • Please print the query that is genereated. Commented Feb 9, 2011 at 1:02
  • Try to echo exact query which you're trying to perform. Commented Feb 9, 2011 at 1:02
  • @zerkms sorry, it got pasted very ugly. added below :P Commented Feb 9, 2011 at 1:04
  • possible duplicate of Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result Commented Aug 8, 2012 at 15:23

4 Answers 4

3

An error like that almost always means there's a problem with your query.

To find out what error message MySQL returns, you can put or die(mysql_error()) just before the semicolon after your mysql_query call. You may also want to print the exact query text you send to MySQL, as this may help you to see the actual problem.

Given that I don't know how much you may have anonymized this example, I can't be sure if this is the actual error, but it looks like you've surrounded your table name with apostrophes ('). This is incorrect; the correct character for escaping table and column names is `.

Sign up to request clarification or add additional context in comments.

2 Comments

Also it is possible that $num_displayed is not defined.
You are correct. Apostrophes (') were killing it. (`) fixed it immediately.
0

"supplied argument is not a valid MySQL result resource" means that the query didn't return a valid resource. It failed. To view the error just print out mysql_error() after mysql_query().

Could be that the table doesn't exist. Did you check it?

The second thinkg - ORDER BY RAND() is bad! You should think of different ways on how to shuffle the result. Just google it, there are a lot of other ways to do it - ORDER BY RAND() @ Google

1 Comment

"The second thinkg - ORDER BY RAND() is bad!" --- he is newbie. He cannot even perform simplest query, and you're trying to teach him about performant randomizing :-S "that the table" --- that "table" doesn't exist, since table cannot be specified with single quotes around.
0

Try changing this line:

$result = mysql_query ("SELECT * FROM 'links' ORDER BY RAND() LIMIT $num_displayed"); 

To:

$result = mysql_query ("SELECT * FROM 'links' ORDER BY RAND() LIMIT $num_displayed") or die (echo mysql_error());

It seems like the SQL is failing to return a valid response. Adding the above should show you the last MySQL error and help point you in the correct direction.

Comments

0

Please add this code to get if the mysql is throwing any errors:

  $result = mysql_query('SELECT * WHERE 1=1');
    if (!$result) {
        die('Invalid query: ' . mysql_error());
    }

while(....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.