In the Mysql database, how can we obtain random Mysql Data in a table? The following describes how to obtain Mysql Data randomly.
- header("Content-type:text/html;charset=utf-8");
- $mysql_host = 'localhost';
- $mysql_un = 'root';
- $mysql_pwd = '123456';
- $my_db_name = 'mytestdb';
- $my_table_name = 'mytesttable';
- $output_str = '';
- $resource = mysql_connect($mysql_host,$mysql_un,$mysql_pwd) or die('Mysql connect error!');
- mysql_select_db($my_db_name,$resource);
- $result = mysql_query("select id from $my_table_name",$resource) or die('select data error');
- $my_id_array = array();
- while($row = mysql_fetch_assoc($result)){
- if(is_numeric($row['id'])){
- $my_id_array[] = $row['id'];
- }
- }
- mysql_free_result($result);
- if(count($my_id_array)>0){
- $rand_query_id = array_rand($my_id_array,1);
- $result = mysql_query("select * from $my_table_name where id = $rand_query_id",$resource) or die('select data error2');
- $row = mysql_fetch_assoc($result);
- $output_str = "
{$row['foodname']} |
{$row['price']} |
{$row['tel']} |
";
- mysql_free_result($result);
- }else{
- $output_str = 'Without any data';
- }
- mysql_close($resource);
- echo $output_str;
- ?>
The preceding describes how to obtain Mysql Data randomly.
Provides you with an in-depth understanding of Mysql temporary tables.
MySQL Show statement usage
How to clear a table in Mysql
Command for viewing the Mysql table structure
MySQL connection query Overview