将 MySQL 查询的每个单独结果分配到唯一数组的最简单方法是什么?

发布于 2024-11-05 16:52:13 字数 812 浏览 2 评论 0原文

基本上我想在文档的其他地方调用这些变量,但我不确定最简单的方法是什么。例如,在我的具体情况下,我的查询是这样的:

     $query = "SELECT report,";
        $query.= "GROUP_CONCAT(DISTINCT analyst) AS analysts, ";
        $query.= "GROUP_CONCAT(DISTINCT region) AS regions, ";
        $query.= "GROUP_CONCAT(DISTINCT country) AS countries, ";
        $query.= "GROUP_CONCAT(DISTINCT topic) AS topics, ";
        $query.= "GROUP_CONCAT(DISTINCT date) AS dates, ";
        $query.= "GROUP_CONCAT(DISTINCT province) AS provinces ";
        $query.= "FROM reports GROUP BY report ORDER BY docID DESC ";

      $result = mysql_query($query);

  while($row=mysql_fetch_assoc($result)) { yada yada yada }

我希望将每个单独的 $row 保存为唯一的数组,我可以在文档中的其他位置调用它,在我的情况下,我尝试将它们称为默认值对于某些表单输入。我可以想到几种方法来做到这一点,但它们看起来都很复杂,添加一些嵌套的 for 语句,并增加 i...

有什么想法吗?

Basically I want to call these variables at other places in the document, but I am not sure what the easiest way to do this would be. For example in my specific case, my query is this:

     $query = "SELECT report,";
        $query.= "GROUP_CONCAT(DISTINCT analyst) AS analysts, ";
        $query.= "GROUP_CONCAT(DISTINCT region) AS regions, ";
        $query.= "GROUP_CONCAT(DISTINCT country) AS countries, ";
        $query.= "GROUP_CONCAT(DISTINCT topic) AS topics, ";
        $query.= "GROUP_CONCAT(DISTINCT date) AS dates, ";
        $query.= "GROUP_CONCAT(DISTINCT province) AS provinces ";
        $query.= "FROM reports GROUP BY report ORDER BY docID DESC ";

      $result = mysql_query($query);

  while($row=mysql_fetch_assoc($result)) { yada yada yada }

I would like to have each individual $row save as a unique array where I can call it someplace else in the document, in my case, I am trying to call them as default values for some form inputs. I can think of several ways to do this, but they all look quite involved, adding some nested for statements, and incrementing i...

Any ideas?

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

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

发布评论

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

评论(2

当梦初醒 2024-11-12 16:52:13

Which为

$data = array()
while($row = mysql_fetch_assoc($result)) {
    $data[] = $row;
}

您提供了一个父 $data 数组,其中的每个元素都是您获取的关联数组之一。

how about

$data = array()
while($row = mysql_fetch_assoc($result)) {
    $data[] = $row;
}

Which gives you a parent $data array, where each element in it is one of the associative arrays you fetched.

无可置疑 2024-11-12 16:52:13

假设每个生成的数据库行都是唯一的,每个 $row 将在内容方面是唯一的。将行粘贴到名为 $rows 的数组中,如下所示 -

$rows = array();
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
  $rows[] = $row;
  ...
}

如果您正在谈论将每一行命名为唯一变量,那么您可以做一些事情,但是您会留下不可预测的变量名称来尝试弄清楚,所以我希望这不是你的意思:)

Each $row will be unique, contents-wise, assuming each resulting database row is unique. Stick the rows into an array called $rows like so -

$rows = array();
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
  $rows[] = $row;
  ...
}

If you're talking about naming each row as a unique variable, there are things you could do, but then you're left with unpredictable variable names to try and figure out, so I'm hoping that's NOT what you mean :)

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