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)
//纯手写的,不明白可以问我,记得给分
1)随机取出5条记录用sql语句就行了mysql如下:"select
*
from
youtable
order
by
rand()
limit
5"
2)同样随机取5-10条:
$num=rand(5,10)
$sql="select
*
from
youtable
order
by
rand()
limit
$num"
ps:为什么不是php分类问题?
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)