0

为什么这会产生一个对象?

$sql = $this->db->query("
  SELECT 'product_id'
  FROM " . DB_PREFIX . "product_to_category
  WHERE category_id = '" . $this->db->escape($category['category_id']) . "'
");

以及如何获取字符串/数组?

来自 Opencart 相关:stdClass 类的对象无法转换为字符串

4

3 回答 3

2

假设 CodeIgniter:

http://codeigniter.com/user_guide/database/queries.html

$this->db->query();

query() 函数在运行“读取”类型查询时返回一个数据库结果对象,您可以使用它来显示结果。

您可以使用以下方法将结果集行作为数组处理result_array

http://codeigniter.com/user_guide/database/results.html

$query = $this->db->query("YOUR QUERY");

foreach ($query->result_array() as $row){
   echo $row['title'];
   echo $row['name'];
   echo $row['body'];
}
于 2012-06-25T17:41:03.897 回答
1
$sql = $this->db->query("
  SELECT 'product_id'
  FROM " . DB_PREFIX . "product_to_category
  WHERE category_id = '" . $this->db->escape($category['category_id']) . "'
");
$rows = $sql->rows; //array of all returned values
$row = $sql->row; //first row

我强烈建议您使用var_dump($sql)来调查哪些公共对象字段可能对您有用。

*固定语法

于 2012-06-25T18:09:49.567 回答
0

您的查询语法有错误,您输入了 SELECT 'product_id' 并且您不应该有撇号。

$sql = $this->db->query("
  SELECT 'product_id'
  FROM " . DB_PREFIX . "product_to_category
  WHERE category_id = '" . $this->db->escape($category['category_id']) . "'
");

您需要进行此更改。

$sql = $this->db->query("
  SELECT product_id
  FROM " . DB_PREFIX . "product_to_category
  WHERE category_id = '" . $this->db->escape($category['category_id']) . "'
");
于 2012-06-26T19:00:41.880 回答