3

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] => [email protected]
    [email] => [email protected]
    [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

enter image description here

enter image description here

6
  • Do the query in your Server Manager again and paste in the results so we can see them. Commented Mar 19, 2013 at 5:50
  • Looks like 26 is the id and 21 is the "foreign key" Commented Mar 19, 2013 at 5:53
  • 1
    Please list all the columns returned by the query. Commented Mar 19, 2013 at 5:53
  • Above is an edit where I've added all the columns in the array, thus all the columns in my table. Commented Mar 19, 2013 at 5:57
  • 1
    Nathan, we need to see what the column names look like before PHP places them into an array. It would be more helpful to see the full results from SQL Server. Commented Mar 19, 2013 at 6:02

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't realize until now. You are absolutely right. There were actually a few things going on here. My initial query, the one posted in my question, was being over written by a different query with the same variable name in a different part of my script. That query had an inner join which was over ridding my array. Thank you so much for your solution. You were SPOT ON!
0

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

1 Comment

No dice, they are still not matching. They being the id value in the array and the table's id value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.