将 SQL 中的数据存储在数组中

发布于 2024-11-07 15:44:39 字数 371 浏览 0 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梓梦 2024-11-14 15:44:39

如果要将所有名称存储在数组中,则需要在 while 循环范围之外定义该数组并将其追加到该数组中。像这样:

$nameArray = array();
while($row = mysql_fetch_array($query)) {
    // Append to the array
    $nameArray[] = $row['name'];   
}

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:

$nameArray = array();
while($row = mysql_fetch_array($query)) {
    // Append to the array
    $nameArray[] = $row['name'];   
}
ら栖息 2024-11-14 15:44:39

您想要的是:

$inspector[] = $row['name'];

这会将所有 8 个名称存储在一个数组中,类似于:

array(
     [0] => name1
     [1] => name2
     [2] => name3

)

What you want is:

$inspector[] = $row['name'];

This will store all 8 names in an array similar to:

array(
     [0] => name1
     [1] => name2
     [2] => name3

)
对你的占有欲 2024-11-14 15:44:39

很多好的答案。但如果您经常这样做,您可能需要编写一个小函数:

mysql_field_array($sql, $fieldname)
{
    $res = mysql_query($sql);
    $a = array();
    while($row = mysql_fetch_array($res))
    {
        $a[] = $row[$fieldname];
    }
    mysql_free_result($res);
    return $a;
}

Lots of good answers. But if you do this often, you might want to write a little function:

mysql_field_array($sql, $fieldname)
{
    $res = mysql_query($sql);
    $a = array();
    while($row = mysql_fetch_array($res))
    {
        $a[] = $row[$fieldname];
    }
    mysql_free_result($res);
    return $a;
}
黄昏下泛黄的笔记 2024-11-14 15:44:39

将此行...

$inspector = $row['name'];

...替换为:

$inspector [] = $row['name'];

之后,检查器数组包含所有名称。

Replace this line...

$inspector = $row['name'];

...with this:

$inspector [] = $row['name'];

After that, the inspector array contains all the names.

鹊巢 2024-11-14 15:44:39
$query = mysql_query("SELECT * FROM  `InspEmail` WHERE  `Company` LIKE  '$company'");
$data = array();
while($row = mysql_fetch_array($query))
{
    $inspector[] = $row;
}
for($i=0;$i<mysql_num_rows($row);$i++)
{
  $data = $inspector[$i];
}
return $data;

检查一下...

$query = mysql_query("SELECT * FROM  `InspEmail` WHERE  `Company` LIKE  '$company'");
$data = array();
while($row = mysql_fetch_array($query))
{
    $inspector[] = $row;
}
for($i=0;$i<mysql_num_rows($row);$i++)
{
  $data = $inspector[$i];
}
return $data;

Check it...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文