我有一个包含四列的 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..);
最佳答案
基础知识:
$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'];
}
关于php - MySQL。随机行。将行转换为 PHP 数组。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8896424/