hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : Picking Random data from mySQL
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

Picking Random data from mySQL

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 09-02-2002, 08:32 PM
CChard CChard is offline
WHT Addict
 
Join Date: Mar 2002
Posts: 123

Picking Random data from mySQL


How do I get a random data entry from a row?

Like I have a table with ID, Name, URL.

I want to pick a random ID from just the ones in the table and echo its Namd and URL to the page.

__________________
www.atlanticrail.com

Reply With Quote


Sponsored Links
  #2  
Old 09-02-2002, 09:13 PM
The Prohacker The Prohacker is offline
iNET Interactive
 
Join Date: May 2001
Location: Dayton, Ohio
Posts: 4,869
PHP Code:
<?php

$id 
rand(0,4);
$sql_info mysql_query("SELECT * FROM info WHERE id='$id'",$sql_con);

?>
This is very rough, and won't work without the rest of the info...

This assumes you have 5 rows in your db, just increase 4 to how many random things you have...

You might have to use another PHP function to get the number of rows in the table to set the max for the random num..

But this should give you a decent idea how todo it..

__________________
-Mat

Reply With Quote
  #3  
Old 09-02-2002, 09:21 PM
Noldar Noldar is offline
Web Hosting Evangelist
 
Join Date: Dec 2001
Location: Ponchatoula, LA
Posts: 497
Here's how I've done it in the past.
Code:
$sql = "SELECT name, URL FROM some_table ORDER BY rand() LIMIT 1";
$result = mysql_query($sql, $connection);
$row = mysql_fetch_object($result);
echo "$row->name $row->URL";
Richard

__________________
Enigma Hosting
"I wasn't speeding, I was qualifying!"

Reply With Quote
Sponsored Links
  #4  
Old 09-02-2002, 11:03 PM
jtrovato jtrovato is offline
Aspiring Evangelist
 
Join Date: Aug 2002
Location: Long Island
Posts: 427
I like the last one.. that is very simple and clean.

__________________
John Trovato
In Office Networks, LLC
Programmer, Cisco Network Engineer, Roofer, Biochemist, and Conductor.

Reply With Quote
  #5  
Old 09-03-2002, 03:26 PM
CChard CChard is offline
WHT Addict
 
Join Date: Mar 2002
Posts: 123
Thanks.

The first one won't work becuase of how often I might be changing the files.

But the second one should be great!

__________________
www.atlanticrail.com

Reply With Quote
  #6  
Old 09-03-2002, 04:35 PM
DimaDubin DimaDubin is offline
Newbie
 
Join Date: Aug 2002
Location: Israel
Posts: 26
PHP Code:
SELECT FROM table ORDER BY RAND() 

Reply With Quote
  #7  
Old 09-04-2002, 05:19 AM
Rich2k Rich2k is offline
Web Hosting Master
 
Join Date: May 2002
Location: UK
Posts: 2,994
Although all the later versions support RAND() I believe some of the version still floating around on some hosts don't support it.

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
FireHost Report Shows SQL Injections Up 69 Percent Over Q1 2012 Web Hosting News 2012-07-24 16:48:13
Web Host Rackspace Launches Private Beta for MySQL Cloud Database Web Hosting News 2011-12-01 21:09:51
CloudSigma Launches SSD Storage Solution for Public Cloud Environment Web Hosting News 2012-12-20 13:35:14
Web Host FireHost Partners with Cloud Security Firm Gazzang for Data Encryption Web Hosting News 2011-08-16 20:33:43
Gazzang Data Encrytion Solution Adds Support for CloudLinux Platform Web Hosting News 2011-06-30 18:54:10


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

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 Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?