4WebHelp
 FAQ  •  Search  •  User Groups  •  Forum Admins  •  Smilies List  •  Statistics  •  Rules   •  Login   •  Register
Toggle Navigation Menu

 loading data from a mysql database into a PHP array
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic
Author Message
domuk
WebHelper
WebHelper


Joined: 15 Jan 2004
Posts: 91

PostPosted: Tue Aug 17, 2004 4:12 pm (20 years, 4 months ago) Reply with QuoteBack to Top

Hi all,

I currently have a problem that I hope you may be able to give me some pointers on.

I shall explain what I am trying to do and how I am trying (unsuccessfully) to achieve it.

The data that I want to manipulate is say:

Table "names"

name_id | __ first
(pri key)
-------------------------------
1 _____ | __ dom
2 _____ | __ john
3 _____ | __ jo
5 _____ | __ jason
6 _____ | __ dave

Note: omission of 4

I am currently querying the database to find out how many names are in the database (in this case 5), then I am generating a random integer between 1 and 5 and using that integer $x to query the database a second time:

query2

SELECT first
FROM names
WHERE name_id='$x'


This gives me one result to which I assign to a variable and do stuff with.

The problem that I am having is in the random number generation

Code:
$x = rand(1, $num2);   


($num2 is from the first query and is the number names in the database)

As $num2 is the total number of names I can get $x = 4 (according to the code this is a valid value for $x, but when the second query comes along 4 is not a valid name_id in the table).

I can not control name_id number order as names can be deleted. Therefore
I am trying to find simple (worked) examples of loading data (eg name_id & first) from a mysql database into a PHP array and then randomly selecting the data based on a random key. I think this will give me more flexibility.

Could anyone first confirm that this is the way to go and then help me with some example code (or links to worked examples).

Thanks again,

Dom.
OfflineView User's ProfileFind all posts by domukSend Personal Message
adam
Forum Moderator & Developer



Joined: 26 Jul 2002
Posts: 704
Location: UK

PostPosted: Wed Aug 18, 2004 9:54 pm (20 years, 4 months ago) Reply with QuoteBack to Top

From what you've said, I gather that you just want to select a random row? If so you can do something like:
Code:
 SELECT * FROM names ORDER BY RAND() LIMIT 1

________________________________
It's turtles all the way down...
OfflineView User's ProfileFind all posts by adamSend Personal MessageVisit Poster's Website
domuk
WebHelper
WebHelper


Joined: 15 Jan 2004
Posts: 91

PostPosted: Thu Aug 19, 2004 8:00 am (20 years, 4 months ago) Reply with QuoteBack to Top

Thanks a lot Adam,

After some thought and writing it down, I was thinking along the same lines.

It also simplifies the problem..

Thanks again Wink

Dom.
OfflineView User's ProfileFind all posts by domukSend Personal Message
Display posts from previous:      
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic


 Jump to:   




You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot edit your posts in this forum.
You cannot delete your posts in this forum.
You cannot vote in polls in this forum.


Page generation time: 0.120502 seconds :: 17 queries executed :: All Times are GMT
Powered by phpBB 2.0 © 2001, 2002 phpBB Group :: Based on an FI Theme