【问题标题】:PHP PDO MYSQL - Get Array comma separated from fetchAllPHP PDO MYSQL - 获取与 fetchAll 分隔的数组逗号
【发布时间】:2015-04-25 18:01:44
【问题描述】:

我真的陷入了困境。

我需要一个类似 (138,139,140,​​141,142,143,144,145) 的结果,但我得到的只是一个 (Array,Array,Array,Array,Array,Array,Array,Array)...

我的代码是:

<?php
    try {
                        $dbconn = DBCONNECTION();

                        $sql = "SELECT `product_category_id`, `product_category_parent_id`, `date_available`, `status` 
                                FROM `product_category` 
                                WHERE `product_category_parent_id` = '" . $_GET['cat'] . "' 
                                AND `date_available` <= NOW() 
                                AND `status` = '1' 
                                ORDER BY `product_category_id` ASC";

                        $stmt = $dbconn -> prepare($sql);
                        $stmt -> execute();
                        $array = $stmt -> fetchAll(PDO::FETCH_ASSOC);

                        foreach($array as $row) {
                            $category[] = array($row['product_category_id']);
                        }

                        $subcategory = implode(',', $category);
                        echo $subcategory;
    }
?>

你能帮帮我吗?

非常感谢!

【问题讨论】:

  • 您的代码对注入开放。不要将用户输入直接放入您的查询中。将占位符 ? 放在它应该去的地方,然后将 $_GET['cat'] 作为数组传入执行中。 stackoverflow.com/questions/60174/…

标签: php mysql arrays pdo


【解决方案1】:

改变

$category[] = array($row['product_category_id']);

$category[] = $row['product_category_id'];

【讨论】:

  • 天啊...这么简单...!非常感谢@panther!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 2014-11-05
  • 1970-01-01
  • 1970-01-01
  • 2011-09-07
相关资源
最近更新 更多