1

I am trying to get MySQL to put everything into a array which would be easier for me to work with later down the road.

I currently am using the following

$result = mysql_query('SELECT * FROM  (SELECT * FROM new WHERE qc_status =\'pending\' AND call_date = \''.date("Y-m-d").'\' LIMIT 0,17) as assesmenttable ORDER BY RAND() LIMIT 1',$link);
                        $array = array();

                        while($row = mysql_fetch_array($result)){
                            foreach($row as $column => $value) {
                                $array[$column]= $value;
                            }
                        }
                        print_r($array);
                }

but the issue is it is giving me an array like this

Array ( [0] => Ms [title] => Ms [1] => Belinda [fname] => Belinda

clearly it is doing something wrong; I want the array to look like this

array([title]=>Ms, [fname]=>Belinda)

In json_encode it should look like this

{title:Ms,fname:Belinda}

Would someone point me in the right direction?

2
  • mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both use mysql_assoc instead Commented Jun 4, 2013 at 6:28
  • 1
    You can use MYSQL_ASSOC as 2nd param for mysql_fetch_array() or use mysql_fetch_assoc(), but please take time to read the big red warning in the manual about mysql_ functions. Commented Jun 4, 2013 at 6:30

1 Answer 1

2

mysql_fetch_array is using MYSQL_BOTH as the result_type by default giving you both - associative and number indices.

You have to use MYSQL_ASSOC as the result_type to get your result array with only associative indices:

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  // your code
}
Sign up to request clarification or add additional context in comments.

Comments

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.