[英]Php array from mysql column
I looked at this question: Create PHP array from MySQL column and what seems to work for everyone is this: 我看着这个问题: 从MySQL专栏创建PHP数组,似乎对每个人都有效的是:
$array= array();
while ($row = mysql_fetch_array(mysql_query("SELECT Username FROM inloggen"))) {
$array[] = $row['Username'];
}
But when I run this code it infinitely adds the first username in my database to the array. 但是,当我运行此代码时,它将无限地将数据库中的第一个用户名添加到数组中。 Does anyone know what I'm doing wrong?
有人知道我在做什么错吗?
You're re-executing the query endlessly, because you're doing it as part of your while, so if any records are returned , your code will re-query and return the same result time and again 您将无休止地重新执行查询,因为您正在执行此查询,因此,如果返回任何记录,您的代码将重新查询并一次又一次返回相同的结果
Execute the query, then iterate over the result set 执行查询,然后遍历结果集
$result = mysql_query("SELECT Username FROM inloggen");
$array = array();
while ($row = mysql_fetch_array($result)) {
$array[] = $row['Username'];
}
Caveat: The MySQL extension is a deprecated interface; 警告:MySQL扩展是已弃用的接口; you should be using MySQLi or PDO
您应该使用MySQLi或PDO
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.