在循环中添加到数组
如何将数据添加到关联数组?我想使用 jQuery 根据键检索数据。
if(isset($_POST["user_name"]))
{
$sql = "SELECT * FROM users WHERE user_name='".$_POST["user_name"]."' AND user_password='".$_POST["user_password"]."'";
$result = mysql_query($sql) or die(mysql_error());
$jsonresult = array();
while($row = mysql_fetch_array($result))
{
$jsonresult["user_auth"] = 1;
$jsonresult["user_id"] = $row['user_id'];
$jsonresult["user_name"] = $row['user_name'];
$_SESSION["user_auth"] = 1;
$_SESSION["user_id"] = $row['user_id'];
$_SESSION["user_name"] = $row['user_name'];
}
echo json_encode($jsonresult);
mysql_close();
}
我的问题是:
$jsonresult["user_auth"] = 1;
$jsonresult["user_id"] = $row['user_id'];
$jsonresult["user_name"] = $row['user_name'];
数据库中只剩下最后一行。为什么?
How can I add data to an associative array? Using jQuery I would like to retrieve data according to a key.
if(isset($_POST["user_name"]))
{
$sql = "SELECT * FROM users WHERE user_name='".$_POST["user_name"]."' AND user_password='".$_POST["user_password"]."'";
$result = mysql_query($sql) or die(mysql_error());
$jsonresult = array();
while($row = mysql_fetch_array($result))
{
$jsonresult["user_auth"] = 1;
$jsonresult["user_id"] = $row['user_id'];
$jsonresult["user_name"] = $row['user_name'];
$_SESSION["user_auth"] = 1;
$_SESSION["user_id"] = $row['user_id'];
$_SESSION["user_name"] = $row['user_name'];
}
echo json_encode($jsonresult);
mysql_close();
}
My problem is here :
$jsonresult["user_auth"] = 1;
$jsonresult["user_id"] = $row['user_id'];
$jsonresult["user_name"] = $row['user_name'];
There remains only the last row from the database. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
好吧,您正在覆盖该值而不是添加新值。相反,为每个结果构造一个数组,并将这些数组添加到
$jsonresult
。另外,请确保避免 SQL 注入漏洞 :
Well, you're overwriting the value instead of adding a new one. Instead, construct an array for each result, and add those arrays to
$jsonresult
.Also, make sure to avoid SQL Injection vulnerabilities:
编辑 首先,我认为您只期望一个结果,因此不需要循环。
其次,您的数据库中有多少用户?您确定它每次都被设置,还是上次运行的残留?尝试这个修改:
看看你是否得到“not”和“set”
Edit Firstly, I think you are expecting only one result, so no need for a loop.
Secondly, how many users do you have in your database? Are you sure it's being set each time, or is it residual from the previous run? Try this modification:
and see if you get "not" and "set"
使用 MYSQL_ASSOC 标志
,甚至用
mysql_fetch_assoc
代替mysql_fetch_array
,不带任何标志。编辑:由于您的函数看起来像基本的身份验证函数,我会将其更改为如下所示:
Use MYSQL_ASSOC flag
Or even
mysql_fetch_assoc
in place ofmysql_fetch_array
, without any flags.EDIT: As your function looks like basic autentification function, I would change it to something like this:
您必须为每一行使用一个键:
You have to use a key for every row:
你的代码对我来说毫无意义。
以下似乎足够了
your code makes no sense to me.
following seems enough
另一种方法是更改查询以仅选择所需的字段(确保清理用户输入!):
初始化数组:
然后使用 mysql_fetch_assoc :
更新:正如@Robert 已经提到的,你实际上应该只得到一个结果,所以不需要循环。
Another way would be to change your query to only select the fields you need (make sure you sanitize user input!):
Initialize the array:
and then use
mysql_fetch_assoc
:Update: As @Robert already mentioned, you actually should only get one result, so there is no need for a loop.
现在,您只有一个一维数组。这意味着每次执行循环时,数组中的这些位置都会被最新值覆盖。您需要一个二维数组(数组的数组),对于您的情况来说非常简单。
更改:
至:
Right now, you only have a 1 dimensional array. That means that each time the loop executes, those locations in the array are overwritten by the latest value. You need a two dimensional array (array of arrays), and it is very simple in your case.
Change:
To: