Reputation: 97
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);
?>
Upvotes: 1
Views: 410
Reputation: 1610
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(....
Upvotes: 0
Reputation: 55009
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 `
.
Upvotes: 3
Reputation: 9719
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.
Upvotes: 0
Reputation: 17762
"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
Upvotes: 0