MySQL的获取数组添加重复值? [英] MySQL fetch array adds duplicate values?

查看:109
本文介绍了MySQL的获取数组添加重复值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个:

Array
(
[0] => 1
[id] => 1
[1] => 778613c4344dbc9565c359c1154c6a18
[session] => 778613c4344dbc9565c359c1154c6a18
[2] => fn
[first_name] => fn
[3] => ln
[last_name] => ln
[4] => un
[username] => un
[5] => 016e6128e8ca9dda1b310c364d9b622c
[password] => 016e6128e8ca9dda1b310c364d9b622c
[6] => address
[email] => address
[7] => 100
[permission] => 100
[8] => 10
[year_level] => 10
[9] => 
[department] => 
[10] => Sample
[campus] => Sample
[11] => 0
[logo_url] => 0
)

运行此

$user = mysql_fetch_array(mysql_query("SELECT session FROM users WHERE username='$cookie[username]' AND first_name='$cookie[first_name]' AND last_name='$cookie[last_name]' AND campus='$cookie[campus]' AND id='$cookie[id]'")) 

有什么想法为什么要这样重复吗?谢谢

Any ideas why it is duplicating like this? Thanks

我认为主键正在执行此操作.关于如何阻止它的任何想法?

I think the primary key is doing this. Any idea on how to stop it?

推荐答案

如果未将结果类型指定为第二个参数,则 mysql_fetch_array() 将默认为MYSQL_BOTH (引用):

If you don't specify a result type as a second parameter, mysql_fetch_array() will default to MYSQL_BOTH (quoting) :

通过使用MYSQL_BOTH(默认),您将获得一个数组,同时包含 关联索引和数字索引.

By using MYSQL_BOTH (default), you'll get an array with both associative and number indices.


如果这不是您想要的结果,则必须将第二个参数传递给该函数,以指示所需的结果类型.


If this is not what you want, you have to pass a second parameter to that function, to indicate what type of results you want.

例如,仅获取以列名作为键的关联数组:

For example, to only get an associative array with columns names as keys :

$result = mysql_query("SELECT session FROM users WHERE username='$cookie[username]' AND first_name='$cookie[first_name]' AND last_name='$cookie[last_name]' AND campus='$cookie[campus]' AND id='$cookie[id]'");
$user = mysql_fetch_array($result, MYSQL_ASSOC);


作为旁注:


As a sidenote :

  • Make sure you are escaping the variables you inject into your query, to prevent SQL Injections, using, for example, mysql_real_escape_string()
  • Before using mysql_fetch_array(), you should test if mysql_query() was successful,
  • And, especially for a new project, you should use mysqli or PDO, and not the old mysql_* functions -- see Choosing an API.

这篇关于MySQL的获取数组添加重复值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆