2

I need some help here.

I have a mysql table called dictionary like this:

id  word
1 test
2 hello
4 bye
7 werd

In php, I want to query the db so that I can get an array of the id field

so the array would list 1 2 4 7.

With this array, I want to run the array_rand function in php so I can get a random number from 1 2 4 7.

Can someone show me the proper mysql query to list the id field and return it into php as an array? and how I could run that array with random array function.

3 Answers 3

3

No need shuffle this in php, - effective is to

use "SELECT id FROM table ORDER BY rand();"

than take all records and store id into array ...

Sign up to request clarification or add additional context in comments.

Comments

1

Use the shuffle function

Comments

0

If you plan on using the random row and then querying for another, I would just randomize on the MySQL query. Get a count of the rows in the table, create a random integer somewhere between 0 and that count, and then use LIMIT in your MySQL query to return a random row.

If you plan on keeping it all in memory, then David's answer would work better.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.