具有键值的PHP数组不匹配
I have a query that I'm running in php. The DB is SQL Server 2008. The query in PHP is:
"SELECT * FROM applicants WHERE applicants.families_id = '{$family_array['id']}'";
Where the $family_array
id is matched against the families_id
. I get a single row as a result. I save that row to an array in PHP by using the mssql_fetch_array
function. When I print this array I get the following:
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
)
If you notice, the index [0]
does not match the corresponding key value [id]
. Why is this? I've ran the exact same query using the SQL Server Manager and it performs as expected, but when I fetch that array in PHP only the first key value gets skewed. I've tried everything I can think of short of recreating the table. Any ideas?
EDIT: Running mssql_fetch_assoc() gives me the following results:
Array
(
[id] => 21
[user_id] => 21
[mother_fname] => Kristi
You are not including all the pertinent information either in the posted SQL or in the table data/structure you've included. Your query is SELECT * FROM applicants WHERE applicants.families_id = ?
yet the table structure you've posted does not contain a families_id
column (nor is it named applicants
). Nor does it contain much of the data in the posted array, e.g., hash_password
, username
, etc.
From this I deduce that you're actually doing a JOIN on a users
table. What's most likely occurring is that the JOIN is including the id
column from the users
table which is overwriting the id
in the main table (families/applicant, whatever it's called) once the array is built. user_id
is already included in your main table so you should explicitly list the columns in your SQL statement, leaving out the users.id
column.
You need to make it mssql_fetch_array($result, MSSQL_ASSOC)
as MSSQL_BOTH is assumed.
I found the answer here: http://php.net/manual/en/function.mssql-fetch-array.php