开发者

MySQL PHP: Extra Rows

开发者 https://www.devze.com 2023-02-27 01:11 出处:网络
In MySQL the table test looks like this: idnamelftrgt 1Folder 1110 2Child 123 3Child 247 4Child 156 5Child 389

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?


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


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.


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消