好吧所以我对MySQL的一切都有天生的恐惧
这是我的表:
+----+-----------------+------+
| id | name | age |
+----+-----------------+------+
| 1 | Timmy Mellowman | 23 |
| 2 | Jeff Johnson | 18 |
+----+-----------------+------+
$raw = MysqL_query("SELECT * FROM example") or die(MysqL_error());
$row = MysqL_fetch_array($raw);
echo "\n\n";
print_r($row);
所以这是我的输出:
Array
(
[0] => 1
[id] => 1
[1] => Timmy Mellowman
[name] => Timmy Mellowman
[2] => 23
[age] => 23
)
为什么这是输出?这个名字加倍?为什么为[1]和[name]设置名称?
解决方法:
每个PHP docs for MysqL_fetch_array:
Returns an array of strings that corresponds to the fetched row, or
FALSE if there are no more rows. The type of returned array depends on
how result_type is defined. By using MysqL_BOTH (default), you’ll get
an array with both associative and number indices. Using MysqL_ASSOC,
you only get associative indices (as MysqL_fetch_assoc() works), using
MysqL_NUM, you only get number indices (as MysqL_fetch_row() works).
所以在你的行$row = MysqL_fetch_array($raw); $row接收一个具有关联和数字索引的数组,因为这是默认值.如果只想要关联索引,请使用$row = MysqL_fetch_array($raw,MysqL_ASSOC); (或者MysqL_fetch_assoc())或者你只想要数字索引,使用$row = MysqL_fetch_array($raw,MysqL_NUM); (或MysqL_fetch_row()).
要检索所有行,请使用以下内容:
while ($row = MysqL_fetch_array($raw)) {
echo "<p>" . $row['id'] . " - " . $row['name'] . " - " . $row['age'] . "</p>\n";
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。