I found this code here in stackoverflow and it says it can select from a random row database but i am having trouble understanding how the code functions and i also do not know how to apply it to my own php script. Im not sure how i should write it to the syntax and also if i need to add other things to it so i can function to get 10 random rows, also after getting the rows how would i put the results into a csv.output or a variable so i can extract it from my app. I am trying to do what this guy did http://ift.tt/1STz1E2 if you look at his first script it has a csv.output thing and i want the rows gotten to be put into a csv.output.
(this is the section i dont understand how it functions or how to add to the script)
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;
My code:
<?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();
}
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;
?>
Aucun commentaire:
Enregistrer un commentaire