Code
<?php
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 = "<table><tr><td>{$row['foodname']}</td><td>{$row['price']}</td><td>{$row['tel']}</td></tr></table>";
mysql_free_result($result);
}else{
$output_str = 'Without any data';
}
mysql_close($resource);
echo $output_str;
?>
<?php
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 = "<table><tr><td>{$row['foodname']}</td><td>{$row['price']}</td><td>{$row['tel']}</td></tr></table>";
mysql_free_result($result);
}else{
$output_str = 'Without any data';
}
mysql_close($resource);
echo $output_str;
?>
select * from tablename order by rand() limit 1 一句MYSQL其实就可以实现该功能