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);
?>
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 yourmysql_querycall. 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`.