将 SQL 中的数据存储在数组中
我正在尝试将 sql 数据库中的数据存储到数组中。目前我有这个:
$query = mysql_query("SELECT * FROM `InspEmail` WHERE `Company` LIKE '$company'");
while($row = mysql_fetch_array($query))
{
$inspector = $row['name'];
}
问题是我有 8 行数据。我需要将该数据库中的每 8 个名称存储到一个数组中。当我尝试这个时:
$inspector = array($row['name']);
它不起作用。
I am trying to store the data from my sql database into an array. Currently I have this:
$query = mysql_query("SELECT * FROM `InspEmail` WHERE `Company` LIKE '$company'");
while($row = mysql_fetch_array($query))
{
$inspector = $row['name'];
}
The problem is that I have 8 rows of data. I need to store each 8 names from that database into an array. When I try this:
$inspector = array($row['name']);
It doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果要将所有名称存储在数组中,则需要在 while 循环范围之外定义该数组并将其追加到该数组中。像这样:
If you want to store all of the names in an array, you need to define the array outside the scope of the while loop and append to it. Like this:
您想要的是:
这会将所有 8 个名称存储在一个数组中,类似于:
What you want is:
This will store all 8 names in an array similar to:
很多好的答案。但如果您经常这样做,您可能需要编写一个小函数:
Lots of good answers. But if you do this often, you might want to write a little function:
将此行...
...替换为:
之后,检查器数组包含所有名称。
Replace this line...
...with this:
After that, the inspector array contains all the names.
检查一下...
Check it...