Phate Posted October 11, 2007 Share Posted October 11, 2007 Hi, I'm building a small random quote script, and I'm having some troubles starting from the fetch_array. while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { So starting from here, I need to determine how many results my query returned. I don't know how to do this.(for example, save it to $i) Than, it needs to randomly generate a number between 0 and $i. I don't know, how to do that . Than, it needs to select just that result from the query and display it. Any help would be appreciated. Thanks, Phate Quote Link to comment https://forums.phpfreaks.com/topic/72760-solved-random-select/ Share on other sites More sharing options...
Orio Posted October 11, 2007 Share Posted October 11, 2007 Instead of pulling all of the records and then choosing one, you can simply pull one random row: SELECT quote FROM quote_table ORDER BY RAND() LIMIT 1 I dont think you should pull all of the records and then select one because this way both your sql query will be more complex and php will have to handle more data and use more memory... But if you still want to pull them all and then select one random row, I'd recommend using: <?php $quote = mysql_result($result, "quote_column", rand(0, mysql_num_rows($result))); ?> Again, you don't want to do it this way. Orio. Quote Link to comment https://forums.phpfreaks.com/topic/72760-solved-random-select/#findComment-366930 Share on other sites More sharing options...
Phate Posted October 11, 2007 Author Share Posted October 11, 2007 Thanks, but I think I'm missing something in that query (complete noob here), maybe missing an arguement in the RAND() function? The code doesn't give me any errors, but well, it doesn't show anything at all. Don't worry about the passwords in there, it's on a computer that isn't connected to the web. <? mysql_connect(localhost, quote, quote) or die(mysql_error()); mysql_select_db(quotes); $query = "SELECT quote FROM quotes ORDER BY RAND() LIMIT 1"; $result= mysql_query($query); echo $result; ?> Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/72760-solved-random-select/#findComment-366977 Share on other sites More sharing options...
Orio Posted October 11, 2007 Share Posted October 11, 2007 Because mysql_query() doesn't return a string. You need to do it this way: <?php mysql_connect(localhost, quote, quote) or die(mysql_error()); mysql_select_db(quotes); $query = "SELECT quote FROM quotes ORDER BY RAND() LIMIT 1"; $result= mysql_query($query); $row = mysql_fetch_array($result); echo $row['quote']; ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/72760-solved-random-select/#findComment-366985 Share on other sites More sharing options...
Phate Posted October 11, 2007 Author Share Posted October 11, 2007 Code: [code=php:0]<?php mysql_connect(localhost, quote, quote) or die(mysql_error()); mysql_select_db(quotes); $query = "SELECT quote FROM quote ORDER BY RAND() LIMIT 1"; $result= mysql_query($query); $row= mysql_fetch_array($result); echo $row['quote']; ?> [/code] Error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in E:\chiemel\xampp\htdocs\ss\quote.php on line 7 MySQL dump: SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; -- -- Database: `quotes` -- CREATE DATABASE `quotes` DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci; USE `quotes`; -- -------------------------------------------------------- -- -- Table structure for table `quote` -- CREATE TABLE `quote` ( `id` int(11) NOT NULL auto_increment, `quote` varchar(60) collate latin1_general_ci NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=3 ; -- -- Dumping data for table `quote` -- INSERT INTO `quote` (`id`, `quote`) VALUES (1, 'Dissadump!'), (2, 'Test 1'); Thanks for trying to help me Edit: Sorry, made a stupid mistake in the code Everything works now! Thanks alot! Quote Link to comment https://forums.phpfreaks.com/topic/72760-solved-random-select/#findComment-367000 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.