php隨機獲取資料庫資料庫資料庫數據
㈠ PHP從MYSQL資料庫中隨機讀取若干條數據,並將數據合並為一條數據,在賦值給一個變數。要怎麼實現
$sql = "select title from 表名";
$result = mysql_query($sql);
$title = '';
while($res = mysql_fetch_array($result, MYSQL_ASSOC) )
{
$title .= $res['title'];
}
echo $title;
㈡ php讀取mysql資料庫隨機顯示指定數量的記錄
試試,這個想法行吧。
<?php
$datas = 100; //數據表裡100條數據
$setnum = 6; //設置隨顯示6條
$rand = '';
//for循環組建隨機的id
for($i=1;$i<=$setnum;$i++){
$rand .= ",".rand(1,100); //在1~100之間取隨機數包括1和100
}
//獲得隨機ID
$ids = substr($rand,1);
echo $ids;
/*
* 下面用隨機ID查詢數據就可以了
* select * from table where id in($ids);
*/
?>
㈢ php語句,怎麼從資料庫中隨機獲取數據欄位。
//連接資料庫
if(!$con = mysql_connect("localhost","root","root")){die(mysql_error());}
mysql_select_db("ali_xt");
mysql_query('set names utf8');
//找出ali_admin表的欄位
$res = mysql_query('show columns from ali_admin');
//將數據給弄出來
$data = array();
while ($row = mysql_fetch_assoc($res, MYSQL_NUM)) {
$data[] = $row;
}
//隨機個數,默認5
$rand_times = 5;
$rand_times = count($data)<$rand_times?count($data):$rand_times;
$result = array();
for( $i=0;$i<$rand_times;$i++ ){
$result[] = $data[rand(0,count($data)-1)][0];
}
echo "<pre>";
print_r($result); //輸出5個隨機欄位
mysql_close($con);
//純手寫的,不明白可以問我,記得給分
㈣ php程序隨機調用mysql資料庫的問題
select * from tableName where id > 0 AND id < 60 order by rand()
如果id有刪除,用這個:
select * from (select * from tableName order by id ASC limit 0, 60 ) as tmp order by rand();
rand()是 mysql 自己的函數,取出數據後,按隨機排序