Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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}

if anyone can point me in the right direction that would be a great help

share|improve this question
mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both use mysql_assoc instead – sourabh kasliwal 20 hours ago
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. – piotrm 20 hours ago

1 Answer

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
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.