Jump to content

mysql_fetch_array() - output ?


ttmt

Recommended Posts

Hi all,

 

I'm new to php so this could be a really stupid question put I can't see the reason for it.

I have simple one table (users) DB with fields for id, name and age.

 

I'm using this code to get all capture all the info in the table. I have functions before this to connect and select the DB.

 

<pre>
<?php
$all = mysql_query("SELECT * FROM users");
if(!$all){
	die("Query Failed". mysql_error());
}
while($names = mysql_fetch_array($all)){
	print_r($names);
}

?>
</pre>

 

The results I get look like this

 

Array
(
    [0] => 1 // Whats this
    [id] => 1
    [1] => chris // Whats this
    [name] => chris 
    [2] => 25 // Whats this
    [age] => 25
)

 

My problem is I dont understand where [0]=>1, [1] => chris, [2] => 25 (commentd out in the code.) is coming from.

 

I was expecting do get this.

 

Array
(
    [id] => 1
    [name] => chris 
    [age] => 25
)

 

Link to comment
https://forums.phpfreaks.com/topic/135630-mysql_fetch_array-output/
Share on other sites

I was expecting do get this.

 

Array
(
    [id] => 1
    [name] => chris 
    [age] => 25
)

Why were you? The manual states it clearly:

 

mysql_fetch_array

 

(PHP 4, PHP 5, PECL mysql:1.0)

 

mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both

 

Use mysql_fetch_assoc() instead.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.