php获取随机数据mysql,如何使用PHP在MySQL上显示随机数据?

Hello i have tables like this :

Employee

EmployeeID EmployeeName

1234 Nayeon

1235 Jihyo

1236 Jungyeon

1237 Dahyun

1238 Sana

1239 Mina

1240 Tzuyu

1241 Chaeyeong

1241 Chaeyeong

1242 Momo

i used this source code :

mysql_connect("localhost", "root", "1234") or die(mysql_error());

mysql_select_db("databasetransport") or die(mysql_error());

$employees = mysql_query("SELECT * FROM Employee ORDER BY EmployeeID")

or die(mysql_error());

$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

$position = 0;

$position2 = 0;

$toomany = '';

while($row = mysql_fetch_array( $employees )) {

echo "

" . $toomany.substr($letters, $position, 1) . " = " . $row['EmployeeName'] . "
";

$position ++;

if($position > 25) {

$position = 0;

$position2 ++;

if($position2 > 25) { echo "We need to rethink this idea."; break; }

$toomany = substr($letters, $position2, 1);

}

}

?>

to display these data :

A = Nayeon

B = Jihyo

C = Jungyeon

D = Dahyun

E = Sana

F = Mina

G = Tzuyu

F = Chaeyeong

H = Chaeyeong

I = Momo

The problem is i want to random that data like this (from the result before):

C = Jungyeon

A = Nayeon

H = Chaeyeong

B = Jihyo

I = Momo

F = Mina

G = Tzuyu

E = Sana

F = Chaeyeong

D = Dahyun

so i add codes like this :

mysql_connect("localhost", "root", "1234") or die(mysql_error());

mysql_select_db("databasetransport") or die(mysql_error());

$employees = mysql_query("SELECT * FROM Employee ORDER BY EmployeeID")

or die(mysql_error());

$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

$position = 0;

$position2 = 0;

$toomany = '';

while($row = mysql_fetch_array( $employees )) {

echo "

" . $toomany.substr($letters, $position, 1) . " = " . $row['EmployeeID'] . "
";

$position ++;

if($position > 25) {

$position = 0;

$position2 ++;

if($position2 > 25) { echo "We need to rethink this idea."; break; }

$toomany = substr($letters, $position2, 1);

}

}

function generateRandomString($length = 10) {

$characters = $positions;

$charactersLength = strlen($characters);

$randomString = '';

for ($i = 0; $i < $length; $i++) {

$randomString .= $characters[rand(0, $charactersLength - 1)];

}

return $randomString;

}

echo generateRandomString();

?>

but nothing happened (LOL) may you know where is the problem? Thank you

解决方案

Just build your array and then use this

cheers.

$a = array(

'A = Nayeon',

'B = Jihyo',

'C = Jungyeon',

'D = Dahyun',

'E = Sana',

'F = Mina',

'G = Tzuyu',

'F = Chaeyeong',

'H = Chaeyeong',

'I = Momo',

);

shuffle( $a );

var_export( $a );

Outputs:

array (

0 => 'I = Momo',

1 => 'E = Sana',

2 => 'F = Chaeyeong',

3 => 'F = Mina',

4 => 'B = Jihyo',

5 => 'A = Nayeon',

6 => 'C = Jungyeon',

7 => 'G = Tzuyu',

8 => 'D = Dahyun',

9 => 'H = Chaeyeong',

)

As a side here, shuffle does not maintain the array keys, and it modifies the array meaning that the actual return of shuffle is a Boolean ( true | false ) value.

It does however keep the names with the letter assigned ( if that is desirable )

Is simple and fast

Is readable

Insures you don't pull the same row out twice.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值