在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数据的方法的介绍。
【编辑推荐】