Adam Halasz
Adam Halasz

Reputation: 58301

MySQL PHP: Extra Rows

In MySQL the table test looks like this:

id  name         lft      rgt
1   Folder 1        1          10
2   Child 1         2           3
3   Child 2         4           7
4   Child 1         5           6
5   Child 3         8           9

When I do:

$results = mysql_query("SELETCT * FROM test ORDER BY id");
$fetch = mysql_fetch_array($results);

echo "<pre>";
echo print_r($fetch);
echo "</pre>;

The result is:

[0] => 1
[id] => 1
[1] => Folder 1
[name] => Folder 1
[2] => 1
[lft] => 1
[3] => 10
[rgt] => 10

My question is why [0] => 1, [1] => Folder 1, 2] => 1 and [3] => 10 are there? As I see they are somehow linked with the original rows, maybe these are the indexes for them but how should I get rid of them?

Upvotes: 1

Views: 114

Answers (3)

Marc B
Marc B

Reputation: 360602

mysql_fetch_array() has three modes of retrieving data. The default is MYSQL_BOTH, which is what you're seeing - a combo hash+array structure. The other two modes are array-only (MYSQL_NUM) and hash-only (MYSQL_ASSOC).

In other words, these are direct 1:1 correspondence in fetch modes:

mysql_fetch_array($results, MYSQL_NUM) === mysql_fetch_row($results)
mysql_fetch_array($results, MYSQL_ASSOC) === mysql_fetch_assoc($results)
mysql_fetch_array($results)  === mysql_fetch_array($results, MYSQL_BOTH) // default mode.

Upvotes: 2

ANisus
ANisus

Reputation: 77945

The command mysql_fetch_array will fetch you both an associative and a numeric array unless you specify which one you want. Instead, try using:

$fetch = mysql_fetch_assoc($results);

Upvotes: 0

Erik
Erik

Reputation: 4105

That's how mysql_fetch_array works: you can use an index from 0 to 3, or you can use the name.

If you only want to use the name you can use: mysql_fetch_assoc

Upvotes: 3

Related Questions