How do I get a random element in an array and how do I get a random string from mysql?

I was asked to associate things with what the user has in their products

since the database was created this item table stores the category name so i thought to get the category name and store it in an array

and then do a for loop and get one random value from it

and after that get a random row from mysql database so someone has an idea how to do this

In other words, I want to know how to get a random value from an array and a random string from mysql

Thanks in advance.

+3


source to share


3 answers


using

 $rand_keys = array_rand($input, 1);

      

see example http://php.net/manual/en/function.array-rand.php for php and for mysql



 SELECT * FROM tbl_name ORDER BY RAND() LIMIT 1

      

use this

+2


source


To get a random element from an array in PHP, you can use array_rand .

To get a random string from a MySQL query, you can use RAND () so that something like



ORDER BY RAND() LIMIT 1

      

But note that this method in MySQL can have performance implications.

+2


source


quote from http://php.net/manual/de/function.rand.php :

ilya dot iz at i dot ua 27-Jul-2011 08:09 I had to quickly find a random row from a table and came up with this: ...

It should be easy to get a random value from an array with something like rand (0, count (arrayvar))

0


source







All Articles