Select Random

SpaceshipSpaceship
Spacemail by SpaceshipSpacemail by Spaceship
SpaceshipSpaceship
Watch

TwiztedFake

Established Member
Impact
1
I'm trying to pull a random quote from my database but keep getting the following error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/t/w/i/twiztedfake/html/index.php on line 27

Line 27 is:
$row = mysql_fetch_array($result);

PHP:
<?php
$sql = "SELECT * FROM ct_quotes WHERE ID > '0' ORDER by ID RAND LIMIT 1";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
echo stripslashes($row['quote']);
?>

Whats wrong?

**Update

While looking around some resource sites I found a code snipplet that seems to work for it. Here it is:

PHP:
<?php
	$sql = "SELECT * FROM ct_quotes ORDER BY ID";
	$result = mysql_query($sql);
		while ($row = mysql_fetch_array($result))
			{ 
				$row_array[] = $row['quote']; 
			}
		$random_row = $row_array[rand(0, count($row_array) - 1)];
		echo stripslashes($random_row);
?>

If you think there is a better way to do this please let me know.
 
Last edited:
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
First of all find out what the error is:

PHP:
<?php 
$sql = "SELECT * FROM ct_quotes WHERE ID > '0' ORDER by ID RAND LIMIT 1"; 
$result = mysql_query($sql)or die($sql.'<br/>'.mysql_error()); 
$row = mysql_fetch_array($result); 
echo stripslashes($row['quote']); 
?>
 
0
Close
Here is the error after doing that. Even though I got the other way working I still want to figure this out.

SELECT * FROM ct_quotes WHERE ID > '0' ORDER by ID RAND LIMIT 1
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'RAND LIMIT 1' at line 1
 
0
Close
Try this sql:

Code:
SELECT * FROM ct_quotes WHERE ID > '0' ORDER by RAND LIMIT 1
 
0
Close
SELECT * FROM ct_quotes WHERE ID > '0' ORDER by RAND LIMIT 1
Unknown column 'RAND' in 'order clause'

Thats the result.
 
0
Close
How about

Code:
SELECT * FROM ct_quotes WHERE ID > '0' ORDER by RAND() LIMIT 1
 
0
Close
That fixed it! I knew where the error was happening I just couldn't seem to get the correct syntax to make it work.

Thanks
 
0
Close
DomainEasy: white label portfolios are live. Enable yours.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back