Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/72760-solved-random-select/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/72760-solved-random-select/#findComment-366930
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/72760-solved-random-select/#findComment-366977
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/72760-solved-random-select/#findComment-366985
Share on other sites

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 :D Everything works now!

Thanks alot!

 

Link to comment
https://forums.phpfreaks.com/topic/72760-solved-random-select/#findComment-367000
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.