PHP随机MySQL数据 [英] php random mysql data
问题描述
我有一个MySQL数据库,在一个表中有6列.最终将大约有100行,现在我有3行.
I have a MySQL database with 6 columns in a table. There will eventually be about 100 rows, for now I have 3.
列标题:FirstName,SecondName,Sentence1,Sentence2,Sentence3,Sentence4
Column titles: FirstName, SecondName, Sentence1, Sentence2, Sentence3, Sentence4
所有表都设置为VARCHAR
All tables are set to VARCHAR
我想在网页上使用php从每一行中调用随机数据,例如,将row1 FirstName与row3 SecondName和row2 Sentence1等混合并匹配.
我了解到使用php进行随机化更快,但是尽管进行了搜索,但我确实不知道如何做到这一点.
I read it is quicker to randomise using php but I really can't grasp how to do this despite searching.
我可以连接到我的MySQL数据库并使用以下代码获取返回的结果:
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;
请注意,此后还将重复进行另外3或4行
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.
推荐答案
所有告诉您在SQL查询中使用rand的人都没有读过这个问题.对于那些人:询问者希望从行中随机组合数据,而不是随机行.
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];
?>
这篇关于PHP随机MySQL数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!