具有键值的PHP数组不匹配

时间:2013-03-19 05:41:47

标签: php sql arrays

我有一个查询,我在php中运行。数据库是SQL Server 2008。 PHP中的查询是:

"SELECT * FROM applicants WHERE applicants.families_id = '{$family_array['id']}'";

$family_array ID与families_id匹配的位置。结果我得到一行。我使用mssql_fetch_array函数将该行保存到PHP中的数组中。当我打印这个数组时,我得到以下内容:

    Array
(
    [0] => 26
    [id] => 21
    [1] => 21
    [user_id] => 21
    [2] => Kristi
    [mother_fname] => Kristi
    [3] => Lochala
    [mother_lname] => Lochala
    [4] => Nathan
    [father_fname] => Nathan
    [5] => Lochala
    [father_lname] => Lochala
    [6] => 
    [app_emergency] => 
    [7] => 
    [upload_mother_passport] => 
    [8] => 
    [upload_mother_visa] => 
    [9] => 
    [upload_father_passport] => 
    [10] => 0
    [upload_father_visa] => 0
    [11] => nathan-lochala
    [user_added_username] => nathan-lochala
    [12] => Mar 19 2013 01:00:37:660PM
    [user_added_date] => Mar 19 2013 08:48:00:000AM
    [13] => 192.168.88.15
    [user_added_ip] => 192.168.88.15
    [14] => 
    [user_updated_username] => 
    [15] => 
    [user_updated_date] => 
    [16] => 
    [user_updated_ip] => 
    [17] => 21
    [18] => nathan-lochala
    [username] => nathan-lochala
    [19] => b9a234cb37ce2b75d77befecabfa650e39489e0b
    [hash_password] => b9a234cb37ce2b75d77befecabfa650e39489e0b
    [20] => Nathan
    [fname] => Nathan
    [21] => Lochala
    [lname] => Lochala
    [22] => 2
    [num_child] => 2
    [23] => Mar 19 2013 08:48:00:000AM
    [24] => 192.168.88.15
    [25] => 
    [26] => 
    [27] => nathan-lochala@shk.qsi.org
    [email] => nathan-lochala@shk.qsi.org
    [28] => parent
    [access] => parent
)

如果您发现,index [0]与相应的键值[id]不匹配。为什么是这样?我使用SQL Server管理器运行完全相同的查询,它按预期执行,但是当我在PHP中获取该数组时,只有第一个键值变得偏斜。我已经尝试了所有我能想到的重新创建表格的东西。有什么想法吗?

编辑: 运行mssql_fetch_assoc()会给我以下结果:

Array
(
    [id] => 21
    [user_id] => 21
    [mother_fname] => Kristi

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:1)

您未在已发布的SQL或您包含的表格数据/结构中包含所有相关信息。您的查询为SELECT * FROM applicants WHERE applicants.families_id = ?,但您发布的表格结构不包含families_id列(也未命名为applicants)。它也不包含已发布数组中的大部分数据,例如hash_passwordusername等。

由此我推断你实际上在users表上进行了加入。最有可能发生的是 JOIN包含id表中的users列,该表覆盖了主表中的id(系列/申请人,一旦构建阵列,无论它被称为什么。 user_id已经包含在主表中,因此您应该明确列出SQL语句中的列,而忽略users.id列。

答案 1 :(得分:0)

您需要将其设为mssql_fetch_array($result, MSSQL_ASSOC),因为假定为MSSQL_BOTH。

我在这里找到答案: http://php.net/manual/en/function.mssql-fetch-array.php