Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php random mysql data

Tags:

php

random

mysql

I have a MySQL database with 6 columns in a table. There will eventually be about 100 rows, for now I have 3.

Column titles: FirstName, SecondName, Sentence1, Sentence2, Sentence3, Sentence4

All tables are set to VARCHAR

I want to use php on a web page to call random data from each row, eg mix and match row1 FirstName with row3 SecondName and row2 Sentence1 etc.

I read it is quicker to randomise using php but I really can't grasp how to do this despite searching.

I can connect to my MySQL database and get results returned using this code:

    <?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);
    // Loop the recordset $rs
    // Each row will be made into an array ($row) using mysql_fetch_array
    while($row = mysql_fetch_array($rs)) {
    // Write the value of the column FirstName (which is now in the array $row)
    echo $row['FirstName'] . "<br />";
      }
    // Close the database connection
    mysql_close();
    ?>

but this just returns one column of data. I need the random code to be returned in the webpage using something like:

echo $firstname . $lastname . $sentence1 . $sentence2 . $sentence3 . $sentence4;

Note, this will be repeated for another 3 or 4 rows afterwards too

echo $firstname_2 . $lastname_2 . $sentence1_2 . $sentence2_2 . $sentence3_2 . $sentence4_2;

I'm not too hot on arrays but if someone can get me started it would be great, thanks.

like image 620
Sara44 Avatar asked Jun 27 '26 23:06

Sara44


1 Answers

All those telling you to use rand in the SQL query have not read the question. To those people: the asker wants a random combination of data from the rows, not a random row.

Something like this. It will take all the results from the database and echo a totally random combination. I couldn't avoid using arrays as they are super useful.

<?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];

?>
like image 110
MatthewMcGovern Avatar answered Jun 30 '26 11:06

MatthewMcGovern



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!