up2trouble Posted October 14, 2021 Share Posted October 14, 2021 $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 Quote Link to comment https://forums.phpfreaks.com/topic/313997-getting-errors/ Share on other sites More sharing options...
benanamen Posted October 14, 2021 Share Posted October 14, 2021 Two things. You need to upgrade your PHP to a current version. Second thing, mysql_* has been completely removed from PHP. I would suggest you use PDO with Prepared Statements. https://phpdelusions.net/pdo Quote Link to comment https://forums.phpfreaks.com/topic/313997-getting-errors/#findComment-1591068 Share on other sites More sharing options...
up2trouble Posted October 21, 2021 Author Share Posted October 21, 2021 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>"; ?> ?> Quote Link to comment https://forums.phpfreaks.com/topic/313997-getting-errors/#findComment-1591296 Share on other sites More sharing options...
ginerjm Posted October 21, 2021 Share Posted October 21, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313997-getting-errors/#findComment-1591297 Share on other sites More sharing options...
Barand Posted October 21, 2021 Share Posted October 21, 2021 40 minutes ago, ginerjm said: I strongly suggest going all the way to PDO. +1 1 Quote Link to comment https://forums.phpfreaks.com/topic/313997-getting-errors/#findComment-1591299 Share on other sites More sharing options...
ginerjm Posted October 21, 2021 Share Posted October 21, 2021 (edited) 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 October 21, 2021 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/313997-getting-errors/#findComment-1591300 Share on other sites More sharing options...
Barand Posted October 21, 2021 Share Posted October 21, 2021 $num will not contain the record count. Why is that $num being used in the call to RAND() function? Quote Link to comment https://forums.phpfreaks.com/topic/313997-getting-errors/#findComment-1591301 Share on other sites More sharing options...
ginerjm Posted October 22, 2021 Share Posted October 22, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313997-getting-errors/#findComment-1591310 Share on other sites More sharing options...
Barand Posted October 22, 2021 Share Posted October 22, 2021 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"; Quote Link to comment https://forums.phpfreaks.com/topic/313997-getting-errors/#findComment-1591311 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.