mysqli_fetch_array给我重复的行 [英] mysqli_fetch_array Gives Me Duplicate Rows
问题描述
我有这个SQL命令:
SELECT Username FROM Family WHERE Parent = '$Username' LIMIT 10
当我在PHPMyAdmin上运行该命令并在其中放置一些$ Username时,结果如下:
When I run that command on my PHPMyAdmin and put some $Username on it, I have this in result :
+----------+
| Username |
+----------+
| johndoe |
+----------+
但是当我尝试创建这样的功能时:
but when I try to create function like this :
function userParent($Username)
{
global $con;
$Result = mysqli_query($con, "SELECT Username FROM Family WHERE Parent = '$Username' LIMIT 10");
$Result = mysqli_fetch_array($Result);
return $Result;
}
我有这个数组作为回报:
I have this array in return :
Array ( [0] => johndoe [Username] => johndoe )
因此,当我使用如下的foreach循环在脚本上调用"该函数时:
So when I 'call' that function on my script using foreach loop like this :
$Parent = userParent('james');
foreach ($Parent as $value){
echo $value . '<br>';
}
作为回报,我总是有重复的"johndoe".我相信这种重复的行是由mysqli_fetch_array引起的.如何解决来自mysqli_fetch_array的重复行?
I always have duplicate 'johndoe' in return. I believe this duplicate rows caused by mysqli_fetch_array. How to solve this duplicate rows from mysqli_fetch_array?
谢谢
推荐答案
mysqli_fetch_array()
返回MySQL结果的基于关联和基于数字的数组.代替使用 mysqli_fetch_assoc()
.
mysqli_fetch_array()
returns both an associative and numeric based array of your MySQL results. Use mysqli_fetch_assoc()
instead.
这篇关于mysqli_fetch_array给我重复的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!