MySQL的.随机行.将行转换为PHP数组.

Dad*_*daB 1 php mysql

我有一个包含四列的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..);
Run Code Online (Sandbox Code Playgroud)

Mar*_*c B 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'];
}
Run Code Online (Sandbox Code Playgroud)