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
1 _____ |
2 ___ |
3 ___ |
5 ___ |
6 __ |
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:
[color=red][u]query2[color=red]
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.