下面的代码从数据库表中选择行,但我想选择10个随机行并将它们放在输出变量中,如下面的代码所示。特别是csv,但要模仿下面的那个。
$result = mysqli_query($dbc, "SHOW COLUMNS FROM customer");
$numberOfRows = mysqli_num_rows($result);
if ($numberOfRows > 0) {
$values = mysqli_query($dbc, "SELECT * FROM customer");
while ($rowr = mysqli_fetch_row($values)) {
for ($j=0;$j<$numberOfRows;$j++) {
$csv_output .= $rowr[$j].", ";
}
$csv_output .= "\n";
}
}
print $csv_output;
exit;
首先,我想选择十个随机行,然后输出类似上面代码的内容。
我的代码:
<?php
DEFINE ('DBUSER', '');
DEFINE ('DBPW', '');
DEFINE ('DBHOST', '');
DEFINE ('DBNAME', '');
$dbc = mysqli_connect(DBHOST,DBUSER,DBPW);
if (!$dbc) {
die("Database connection failed: " . mysqli_error($dbc));
exit();
}
$dbs = mysqli_select_db($dbc, DBNAME);
if (!$dbs) {
die("Database selection failed: " . mysqli_error($dbc));
exit();
}
$query = 'Select * FROM Funsies
Order By Rand()
Limit 5';
$result = mysqli_query($dbc, $query);
?>
答案 0 :(得分:0)
SELECT name
FROM random AS r1 JOIN
(SELECT (RAND() *
(SELECT MAX(id)
FROM random)) AS id)
AS r2
WHERE r1.id >= r2.id
ORDER BY r1.id ASC
LIMIT 1
INTO OUTFILE 'c:\\users\\desktop\\file_name.csv'
fields TERMINATED BY ',' enclosed by '"'
LINES TERMINATED BY '\r\n';