Mysql
 sql >> Base de données >  >> RDS >> Mysql

données mysql aléatoires php

Tous ceux qui vous disent d'utiliser rand dans la requête SQL n'ont pas lu la question. À ces personnes :le demandeur veut une combinaison aléatoire de données à partir des lignes, pas une ligne aléatoire.

Quelque chose comme ça. Il prendra tous les résultats de la base de données et fera écho à une combinaison totalement aléatoire. Je ne pouvais pas éviter d'utiliser des tableaux car ils sont super utiles.

<?php
// Connect to database server
mysql_connect("localhost", "xxx", "yyy") or die (mysql_error ());
// Select database
mysql_select_db("zzz") or die(mysql_error());
// SQL query
$strSQL = "SELECT * FROM Users";
// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);
// Array to hold all data
$rows = array();
// Loop the recordset $rs
// Each row will be made into an array ($row) using mysql_fetch_array
while($row = mysql_fetch_array($rs)) {
// add row to array.
$rows[] = $row;
  }
// Close the database connection
mysql_close();

// Max rand number
$max = count($rows) - 1;

// print out random combination of data.
echo $rows[rand(0, $max)][0] . " " . $rows[rand(0, $max)][1] . " " . $rows[rand(0, $max)][2] . " " . $rows[rand(0, $max)][3] . " " . $rows[rand(0, $max)][4] . " " . $rows[rand(0, $max)][5];

?>