[英]Database array only returns one row
I am sorry for my lazy title. 我为我的懒惰头衔感到抱歉。 I hope that a moderator could improve it so the database won't get infected. 我希望主持人可以改进它,以免数据库受到感染。
I got the following code ( forum.php ); 我得到了以下代码( forum.php );
<?php
$res = $db->query('
SELECT *
FROM forums_categories
ORDER BY category_id
');
while ($row = $db->fetch_array($res)) {
$categories = array(
'ID' => $row['category_id'],
'NAME' => $row['category_name']
);
echo '<pre>';
print_r($categories);
echo '</pre>';
}
And I got the following database structure; 我得到了以下数据库结构;
|---------------|-------------------|
| category_id | category_name |
|---------------|-------------------|
| 1 | Example 1 |
| 2 | Example 2 |
| 3 | Example 3 |
| 4 | Example 4 |
| 5 | Example 5 |
| 6 | Example 6 |
|---------------|-------------------|
But my array only returns 1 value: 但是我的数组仅返回1值:
Array
(
[ID] => 1
[NAME] => Example 1
)
Oh and if somebody likes to know how my $db->fetch_array
looks like: 哦,如果有人喜欢知道我的$db->fetch_array
样子:
<?php
function fetch_array($result)
{
return mysql_fetch_assoc($result);
}
How can I return all rows in my array? 如何返回数组中的所有行? Thank you for reading and thank you for replying! 感谢您的阅读并感谢您的答复!
You're overwriting the previous value of $categories
on each iteration 您将在每次迭代中覆盖$categories
的先前值
$categories[] = array(
'ID' => $row['category_id'],
'NAME' => $row['category_name']
);
You might also want to initialize an empty array 您可能还想初始化一个空数组
$categories = array();
before your loop to avoid warnings. 在循环之前避免警告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.