从 While 循环填充 PHP 数组

发布于 2024-11-25 02:30:19 字数 182 浏览 2 评论 0原文

如果我从 MySQL 数据库获取数据并使用 while 循环迭代数据,如何将每个数据添加到数组中?

$result = mysql_query("SELECT * FROM `Departments`");
while($row = mysql_fetch_assoc($result))
{

}

If I am fetching data from a MySQL database and using a while loop to iterate through the data how would I add each one to array?

$result = mysql_query("SELECT * FROM `Departments`");
while($row = mysql_fetch_assoc($result))
{

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

梦里南柯 2024-12-02 02:30:19

当您使用 while 循环进行迭代时构建一个数组。

$result = mysql_query("SELECT * FROM `Departments`");
$results = array();
while($row = mysql_fetch_assoc($result))
{
   $results[] = $row;
}

或者,如果您使用 PDO,您可以 自动执行此操作

Build an array up as you iterate with the while loop.

$result = mysql_query("SELECT * FROM `Departments`");
$results = array();
while($row = mysql_fetch_assoc($result))
{
   $results[] = $row;
}

Alternatively, if you used PDO, you could do this automatically.

秉烛思 2024-12-02 02:30:19

这就能解决问题:

$rows = array();
$result = mysql_query("SELECT * FROM `Departments`");
while($row = mysql_fetch_assoc($result))
{
  $rows[] = $row;
}

This will do the trick:

$rows = array();
$result = mysql_query("SELECT * FROM `Departments`");
while($row = mysql_fetch_assoc($result))
{
  $rows[] = $row;
}
月下伊人醉 2024-12-02 02:30:19

这是我创建(多维)数组最快的方法之一。我不确定您是否希望将所有结果合并到一个数组中。

// Read records
$query = "SELECT * FROM `Departments`"; 
$query = mysql_query($query);

// Put them in array
for($i = 0; $array[$i] = mysql_fetch_assoc($query); $i++) ;

// Delete last empty one
array_pop($array);

您可以使用 print_r($array) 查看结果。

This has been one of the fastest ways for me to create (multi-dimensional) arrays. I'm not sure if you want all of your results smooshed into one array or not.

// Read records
$query = "SELECT * FROM `Departments`"; 
$query = mysql_query($query);

// Put them in array
for($i = 0; $array[$i] = mysql_fetch_assoc($query); $i++) ;

// Delete last empty one
array_pop($array);

You can use print_r($array) to see the results.

拥抱没勇气 2024-12-02 02:30:19

我最喜欢的是以下内容;

$result=odbc_exec($conn,$sql);
if ($result) {
    while($found_results[] = odbc_fetch_array($result)) { } 
    array_pop($found_results); //pop off the empty line from while loading
}

您不需要弹出最后一行,但如果省略它,它会留下空白。
显然对于 mysql 来说也是一样的。

my fav is the following;

$result=odbc_exec($conn,$sql);
if ($result) {
    while($found_results[] = odbc_fetch_array($result)) { } 
    array_pop($found_results); //pop off the empty line from while loading
}

you dont need to pop the last line, but it does leave a blank if you omit it.
works the same for mysql obviously.

始终不够 2024-12-02 02:30:19

如果您的 Departments 表中有多个列并且您想要保存在不同的数组中。

$result = mysql_query("SELECT * FROM `Departments`");
$dept_id = array();
$dept_name=array();
while($row = mysql_fetch_assoc($result))
{
//fetches and store all results in id column on your departments table
$dept_id= $row['id'];
//fetches and store all results in name column on your departments table    
$dept_name=$row['name'];
}

If you have multiple columns in your Departments table and you want to save in different array.

$result = mysql_query("SELECT * FROM `Departments`");
$dept_id = array();
$dept_name=array();
while($row = mysql_fetch_assoc($result))
{
//fetches and store all results in id column on your departments table
$dept_id= $row['id'];
//fetches and store all results in name column on your departments table    
$dept_name=$row['name'];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文