Jump to content

Getting errors


up2trouble
 Share

Recommended Posts

$db_table = "quotes";

//********************************
$connection = mysql_connect($db_host,$db_user,$db_pass) OR DIE ('I can not connect to the server because:' . mysql_error());
mysql_select_db($db_name, $connection) or die  ('I can not select the database because:' . mysql_error());

$num = mysql_query("SELECT COUNT * FROM $db_table");
$result = mysql_query("SELECT * FROM $db_table ORDER BY RAND($num) LIMIT 1");
$row = mysql_fetch_array($result);

echo "<font size='10'> {$row['quote']}</font><BR><BR>";
echo "<font size='7'><i>{$row['author']}</i></font>";
?>
?>

errors

Warning: mysql_fetch_array() [function.mysql-fetch-array]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Chicago' for 'CDT/-5.0/DST' instead in /home/justmason/public_html/quotes/quotes.php on line 15

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/justmason/public_html/quotes/quotes.php on line 15

Link to comment
Share on other sites

Upgrade to what version?

How would this be done in mysqli?

//********************************
$db_table = "quotes";

//********************************
$connection = mysql_connect($db_host,$db_user,$db_pass) OR DIE ('I can not connect to the server because:' . mysql_error());
mysql_select_db($db_name, $connection) or die  ('I can not select the database because:' . mysql_error());

$num = mysql_query("SELECT COUNT * FROM $db_table");
$result = mysql_query("SELECT * FROM $db_table ORDER BY RAND($num) LIMIT 1");
$row = mysql_fetch_array($result);

echo "<font size='10'> {$row['quote']}</font><BR><BR>";
echo "<font size='7'><i>{$row['author']}</i></font>";
?>
?>

Link to comment
Share on other sites

If you are going to learn a new db interface I strongly suggest going all the way to PDO.  Much easier and much better than mysqli for sure.

And learn how to use CSS instead of the outdated <font> tag.

And learn not to use the closing php tag.

Link to comment
Share on other sites

I'll let you setup your own module to do the pdo connection.  But the rest of your script is easy.

$q = "SELECT COUNT * FROM $db_table";
$num = $pdo->query($q);
$q = "SELECT quote,author FROM quotes ORDER BY RAND($num) LIMIT 1";
if (!$result = $pdo->query($q))
{
	echo "Error running query";
    exit();
}
list($quote, $author) = $pdo-fetch(PDO::FETCH_NUM);

echo "$quote<BR><BR>";
echo "<i>{$author</i>";

 

Edited by ginerjm
Link to comment
Share on other sites

Hmmm.. Guess I should have corrected his query when I tried to demo using PDO..


OP - the first query is returning an array which you simply used as a singular value in your 2nd query.  That should be

$cnt = $num[0];

and then use $cnt in your call to RAND.  But - you have to re-think how you are using that SQL RAND function.  I think you may want to use the PHP 'rand()' function to generate your number and embed that into an OFFSET clause in your query to get a record at random.  Read up on it in the manual.

Link to comment
Share on other sites

The first query is returning neither a count of records nor an array - it returns a result object

$q = "SELECT COUNT(*) FROM $db_table";
$res = $pdo->query($q);
$num = $res->fetchColumn();

However, that code is totally superfluous - you don't need the count just to get a random record.

$q = "SELECT quote,author FROM quotes ORDER BY RAND() LIMIT 1";

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.