下面是我的表格 这是我的结果 0-1:-6ren">
gpt4 book ai didi

PHP函数数组多次返回一条记录

转载 作者:行者123 更新时间:2023-11-29 07:26:33 24 4
gpt4 key购买 nike

我在下面有我的代码。我有一个返回记录数组的函数“getdet”,但我无法打印所有记录,代码可能只显示第一条记录。请帮助解决这个问题。

<?php

function getdet($qry){
global $con;
$Selecting=mysqli_query($con,$qry);
$Fetch=mysqli_fetch_array($Selecting);
return $Fetch;
}
$qry="SELECT * FROM `tbs_employee` WHERE EmpId='1' limit 1";
$GetData=getdet($qry);
$i=0;
while($GetData){
echo $i."-".$GetData['EmpId'].":".$GetData['EmpName']."<br>";
$i++;
}
?>

下面是我的表格

enter image description here

这是我的结果

0-1:Fst Employee
1-1:Fst Employee
2-1:Fst Employee
3-1:Fst Employee
4-1:Fst Employee
5-1:Fst Employee
6-1:Fst Employee
.
.
.
infinity

最佳答案

您在 getDet 函数中一遍又一遍地运行相同的查询 ($Selecting=mysqli_query($con,$qry);),因此它将永远不要退出 while 循环:

while($GetData){

应该是:

$qry="SELECT * FROM `tbs_employee` WHERE EmpId='1' limit 1";
$Selecting=mysqli_query($con,$qry);
$i=0;
while($GetData = mysqli_fetch_array($Selecting)){
echo $i."-".$GetData['EmpId'].":".$GetData['EmpName']."<br>";
$i++;
}

mysqli_fetch_array 会返回null,如果所有的结果都返回了,那么while循环就可以退出了。

关于PHP函数数组多次返回一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53371484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com