我有一个包含四列的MySQL表submited_name, submited_URL, submited_type, uniqueID
。在这张表中我有大约1000条记录。我需要选择10个随机条目。问题是我需要将选定的随机值拆分为单独的PHP数组。每列一个。我的数组应该是这样的:
$ten_random_names = array ('name26', 'name55', 'name107', ect.);
$ten_random_URL = array ('url for name26', 'url for name55', 'url for name107', ect..);
$ten_random_types = array ('type for name26', 'type for name55', 'type for name107', ect..);
答案 0 :(得分:5)
基础知识:
$sql = "SELECT name, url, type FROM ... WHERE ... ORDER BY random() LIMIT 10"; // inefficient, but works
$results = mysql_query($sql) or die(mysql_error());
$names = $url = $types = array();
while($row = mysql_fetch_assoc($results)) {
$names[] = $row['name'];
$url[] = $row['url'];
$type[] = $row['type'];
}
答案 1 :(得分:2)
$query = "SELECT *
FROM tablename
ORDER BY RAND()
LIMIT 10";
$result = mysql_query($query);
$ten_random_names = $ten_random_URL = $ten_random_types = array();
while ($row = mysql_fetch_assoc($result)) {
$ten_random_names[] = $row['submited_name'];
$ten_random_URL[] = $row['submited_URL'];
$ten_random_types[] = $row['submited_type'];
}