Dynadot โ€” .com Registration $8.99

Select Random

Spaceship Spaceship
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
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
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
•••
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
•••
Try this sql:

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

Thats the result.
 
0
•••
How about

Code:
SELECT * FROM ct_quotes WHERE ID > '0' ORDER by RAND() LIMIT 1
 
0
•••
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
•••
Unstoppable Domains
Domain Recover
DomainEasy โ€” Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back