在Mysql數據庫中,如果想要得到一張表中隨機的Mysql數據,應該如何實現呢?下面就教您一個隨機獲取Mysql數據的方法,供您參考。
- 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;
- ?>
以上就是隨機獲取Mysql數據的方法的介紹。
帶您深入了解Mysql臨時表
MySQL Show語句用法介紹
Mysql清空表的實現方法
查看Mysql表結構的命令
MySQL聯結查詢一覽