php随机mysql数据

Sar*_*a44 2 php mysql random

我有一个MySQL数据库,表中有6列.最终会有大约100行,现在我有3行.

列标题:FirstName,SecondName,Sentence1,Sentence2,Sentence3,Sentence4

所有表都设置为VARCHAR

我想在网页上使用php从每一行调用随机数据,例如混合和匹配row1 FirstName与row3 SecondName和row2 Sentence1等.

我读到使用php随机化更快但我真的无法掌握如何做到这一点,尽管搜索.

我可以连接到我的MySQL数据库并使用以下代码返回结果:

    <?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();
    ?>
Run Code Online (Sandbox Code Playgroud)

但这只返回一列数据.我需要使用以下内容在网页中返回随机代码:

echo $firstname . $lastname . $sentence1 . $sentence2 . $sentence3 . $sentence4;
Run Code Online (Sandbox Code Playgroud)

注意,这也会在之后的另外3或4行重复

echo $firstname_2 . $lastname_2 . $sentence1_2 . $sentence2_2 . $sentence3_2 . $sentence4_2;
Run Code Online (Sandbox Code Playgroud)

我对阵列不太热,但如果有人能让我开始,那就太棒了,谢谢.

Mat*_*ern 5

所有那些告诉你在SQL查询中使用rand的人都没有读过这个问题.对于那些人:提问者想要从行中随机组合数据,而不是随机行.

像这样的东西.它将从数据库中获取所有结果并回显一个完全随机的组合.我无法避免使用数组,因为它们非常有用.

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

?>
Run Code Online (Sandbox Code Playgroud)