NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Select Random

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 10-19-2006, 12:19 PM THREAD STARTER               #1 (permalink)
NamePros Regular
 
TwiztedFake's Avatar
Join Date: Apr 2004
Location: IL
Posts: 348
TwiztedFake is on a distinguished road
 



Select Random


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 Code:
<?php
????: NamePros.com http://www.namepros.com/programming/248875-select-random.html
????: NamePros.com http://www.namepros.com/showthread.php?t=248875
$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 Code:
<?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(0count($row_array) - 1)];
        echo 
stripslashes($random_row);
?>
If you think there is a better way to do this please let me know.
__________________
-TwiztedFake-
RTard Tutorials
Digital-Dummy to Total-Techie
Last edited by TwiztedFake; 10-19-2006 at 01:22 PM.
TwiztedFake is offline  
Old 10-19-2006, 01:31 PM   #2 (permalink)
Senior Member
 
Barrucadu's Avatar
Join Date: Aug 2005
Location: East Yorkshire, England
Posts: 2,689
Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to behold
 




First of all find out what the error is:

PHP Code:
<?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()); 
????: NamePros.com http://www.namepros.com/showthread.php?t=248875
$row mysql_fetch_array($result); 
echo 
stripslashes($row['quote']); 
?>
Barrucadu is offline  
Old 10-19-2006, 02:04 PM THREAD STARTER               #3 (permalink)
NamePros Regular
 
TwiztedFake's Avatar
Join Date: Apr 2004
Location: IL
Posts: 348
TwiztedFake is on a distinguished road
 



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
__________________
-TwiztedFake-
RTard Tutorials
Digital-Dummy to Total-Techie
TwiztedFake is offline  
Old 10-19-2006, 02:06 PM   #4 (permalink)
Senior Member
 
Barrucadu's Avatar
Join Date: Aug 2005
Location: East Yorkshire, England
Posts: 2,689
Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to behold
 




Try this sql:

Code:
SELECT * FROM ct_quotes WHERE ID > '0' ORDER by RAND LIMIT 1
Barrucadu is offline  
Old 10-19-2006, 02:09 PM THREAD STARTER               #5 (permalink)
NamePros Regular
 
TwiztedFake's Avatar
Join Date: Apr 2004
Location: IL
Posts: 348
TwiztedFake is on a distinguished road
 



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

Thats the result.
__________________
-TwiztedFake-
RTard Tutorials
Digital-Dummy to Total-Techie
TwiztedFake is offline  
Old 10-19-2006, 02:11 PM   #6 (permalink)
Senior Member
 
Barrucadu's Avatar
Join Date: Aug 2005
Location: East Yorkshire, England
Posts: 2,689
Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to behold
 




How about

Code:
SELECT * FROM ct_quotes WHERE ID > '0' ORDER by RAND() LIMIT 1
Barrucadu is offline  
Old 10-19-2006, 02:17 PM THREAD STARTER               #7 (permalink)
NamePros Regular
 
TwiztedFake's Avatar
Join Date: Apr 2004
Location: IL
Posts: 348
TwiztedFake is on a distinguished road
 



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
__________________
-TwiztedFake-
RTard Tutorials
Digital-Dummy to Total-Techie
TwiztedFake is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 09:10 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger