dongyun7897 2015-07-09 08:08
浏览 71

如何使用PHP在MySQL上使用随机数据或随机数据?

Hello i have tables like this :

Employee

EmployeeID  EmployeeName 
1234        Suho    
1235        Kai    
1236        Baekhyun    
1237        Lay  
1238        D.O      
1239        Chen
1240        Chanyeol
1241        Xiumin
1242        Sehun

i used this source code to make random data :

<?php

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

$employees = mysql_query("select p.*
from (select (@c := CHAR(ASCII(@c) + 1)) as c, EmployeeName
      from Employee cross join
           (select @c := 'A') params
      order by EmployeeId
     ) p order by rand();") 
or die(mysql_error());  

$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$position = 0;
$position2 = 0;
$toomany = '';

while($row = mysql_fetch_array( $employees )) {
    echo "<DIV>" . $toomany.substr($letters, $position, 1) . " = " . $row['EmployeeName'] . " </div>";
      $position ++;
    if($position > 25) {
        $position = 0;
        $position2 ++;
        if($position2 > 25) { echo "We need to rethink this idea."; break; }
        $toomany = substr($letters, $position2, 1);
    }
}

?>

After random the data, the result is like this :

A  = Sehun     
B  = Suho     
C  = Kai     
D  = D.O     
E  = Chen       
F  = Chanyeol
G  = Baekhyun
F  = Lay
H  = Xiumin

or like this :

A  = Kai     
B  = Suho     
C  = Sehun    
D  = D.O     
E  = Chen       
F  = Xiumin
G  = Lay
F  = Chanyeol
H  = Baekhyun

The problem is i want to random that data like this (From the database before, EmployeeID changed by alphabet sequentially):

A = Suho     
B = Kai    
C = Baekhyun    
D = Lay  
E = D.O      
F = Chen
G = Chanyeol
H = Xiumin
I = Sehun

into this one, the data have with the same value but with a different sequence (Only their potition change, not only their names were randomized) :

G = Chanyeol  
C = Baekhyun   
B = Kai  
D = Lay  
I = Sehun
E = D.O      
F = Chen
A = Suho
H = Xiumin

or like this :

I = Sehun
E = D.O  
C = Baekhyun
G = Chanyeol    
B = Kai 
H = Xiumin 
D = Lay        
F = Chen
A = Suho

May you know where is the problem? Thank you for your help.

  • 写回答

1条回答 默认 最新

  • duanhuo7441 2015-07-09 08:20
    关注

    Get all from the DB and keep it into a PHP Array, then apply one of the randomize PHP Functions. Shuffle changes the keys, so I saw someone who posted a nice function in the PHP.net website which keeps the indexes:

    <?php
        function shuffle_assoc(&$array) {
            $keys = array_keys($array);
    
            shuffle($keys);
    
            foreach($keys as $key) {
                $new[$key] = $array[$key];
            }
    
            $array = $new;
    
            return true;
        }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?