PHP中的rand()函数存在问题

PHP中的rand()函数存在问题,php,mysql,random,fetch,Php,Mysql,Random,Fetch,我的rand()函数有点问题。我有以下疑问: $listTrainers = mysqli_query($conn, "SELECT emp_id FROM employees;"); while($fetchTrainers = mysqli_fetch_row($listTrainers)) { echo 'ID: ' . $fetchTrainers['0']. '<br>'; } 其中,begin是查询中的第一个元素,end是最

我的rand()函数有点问题。我有以下疑问:

$listTrainers = mysqli_query($conn, "SELECT emp_id FROM employees;"); 

    while($fetchTrainers = mysqli_fetch_row($listTrainers))
    {
        echo 'ID: ' . $fetchTrainers['0']. '<br>';

    }

其中,begin是查询中的第一个元素,end是最后一个元素,请将其添加到查询中:

ORDER BY RAND() LIMIT 1

将此添加到您的查询:

ORDER BY RAND() LIMIT 1

您可以在查询中轻松地执行此操作

SELECT emp_id FROM employees ORDER BY RAND() LIMIT 1

您可以在查询中轻松地执行此操作

SELECT emp_id FROM employees ORDER BY RAND() LIMIT 1

最简单的方法是直接在数据库中执行此操作,以避免在只需要一个ID时获取所有ID:

SELECT
    emp_id
FROM
    employees
ORDER BY
    RAND()
LIMIT
    1
如果确实需要所有ID,但还想随机选取一个ID,请使用此选项避免查询数据库两次:

$listTrainers = mysqli_query($conn, "SELECT emp_id FROM employees;"); 

while($fetchTrainers = mysqli_fetch_row($listTrainers))
{
    $id = $fetchTrainers[0];
    echo 'ID: ' . $id . '<br>';
    $ids[] = $id;
}
$randomId = $ids[array_rand($ids)];
$listTrainers=mysqli\u query($conn,“从员工中选择emp\u id;”);
而($fetchTrainers=mysqli\u fetch\u行($listTrainers))
{
$id=$fetchTrainers[0];
回显“ID:”.$ID.“
”; $id[]=$id; } $randomId=$ids[array_rand($ids)];
最简单的方法是直接在数据库中执行此操作,以避免在只需要一个ID时获取所有ID:

SELECT
    emp_id
FROM
    employees
ORDER BY
    RAND()
LIMIT
    1
如果确实需要所有ID,但还想随机选取一个ID,请使用此选项避免查询数据库两次:

$listTrainers = mysqli_query($conn, "SELECT emp_id FROM employees;"); 

while($fetchTrainers = mysqli_fetch_row($listTrainers))
{
    $id = $fetchTrainers[0];
    echo 'ID: ' . $id . '<br>';
    $ids[] = $id;
}
$randomId = $ids[array_rand($ids)];
$listTrainers=mysqli\u query($conn,“从员工中选择emp\u id;”);
而($fetchTrainers=mysqli\u fetch\u行($listTrainers))
{
$id=$fetchTrainers[0];
回显“ID:”.$ID.“
”; $id[]=$id; } $randomId=$ids[array_rand($ids)];
非常好,正是我需要的,而且很简单。谢谢你的帮助,当我表现出色时,我会接受你的回答,这正是我所需要的,也很简单。谢谢你的帮助,我会在可能的时候接受你的回答