[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

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


Closed Thread
 
LinkBack Thread Tools
Old 10-19-2006, 11:19 AM   #1 (permalink)
NamePros Regular
 
TwiztedFake's Avatar
 
Join Date: Apr 2004
Location: IL
Posts: 339
50.00 NP$ (Donate)

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
$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(0, count($row_array) - 1)];
        echo
stripslashes($random_row);
?>
If you think there is a better way to do this please let me know.
__________________
Four Seasons Outfitters
[ 25 $NP/Month ] [ 25 $NP/Month ] [ 25 $NP/Month ]
[ 25 $NP/Month] [ 25 $NP/Month]

Last edited by TwiztedFake; 10-19-2006 at 12:22 PM.
TwiztedFake is offline  
Old 10-19-2006, 12:31 PM   #2 (permalink)
Barru.
 
Barrucadu's Avatar
 
Join Date: Aug 2005
Location: East Yorkshire, England
Posts: 2,731
78.50 NP$ (Donate)

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());
$row = mysql_fetch_array($result);
echo
stripslashes($row['quote']);
?>
Barrucadu is offline  
Old 10-19-2006, 01:04 PM   #3 (permalink)
NamePros Regular
 
TwiztedFake's Avatar
 
Join Date: Apr 2004
Location: IL
Posts: 339
50.00 NP$ (Donate)

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
__________________
Four Seasons Outfitters
[ 25 $NP/Month ] [ 25 $NP/Month ] [ 25 $NP/Month ]
[ 25 $NP/Month] [ 25 $NP/Month]
TwiztedFake is offline  
Old 10-19-2006, 01:06 PM   #4 (permalink)
Barru.
 
Barrucadu's Avatar
 
Join Date: Aug 2005
Location: East Yorkshire, England
Posts: 2,731
78.50 NP$ (Donate)

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, 01:09 PM   #5 (permalink)
NamePros Regular
 
TwiztedFake's Avatar
 
Join Date: Apr 2004
Location: IL
Posts: 339
50.00 NP$ (Donate)

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.
__________________
Four Seasons Outfitters
[ 25 $NP/Month ] [ 25 $NP/Month ] [ 25 $NP/Month ]
[ 25 $NP/Month] [ 25 $NP/Month]
TwiztedFake is offline  
Old 10-19-2006, 01:11 PM   #6 (permalink)
Barru.
 
Barrucadu's Avatar
 
Join Date: Aug 2005
Location: East Yorkshire, England
Posts: 2,731
78.50 NP$ (Donate)

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, 01:17 PM   #7 (permalink)
NamePros Regular
 
TwiztedFake's Avatar
 
Join Date: Apr 2004
Location: IL
Posts: 339
50.00 NP$ (Donate)

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
__________________
Four Seasons Outfitters
[ 25 $NP/Month ] [ 25 $NP/Month ] [ 25 $NP/Month ]
[ 25 $NP/Month] [ 25 $NP/Month]
TwiztedFake is offline  
Closed Thread


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 11:38 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85