3

我用过

 $member_id = 12;

 $results = db_query("select * from {customorders} where id = :fid", array(':fid' => $member_id));
 foreach($results as $result) {
       $name = $result['name'];
 }

但我得到错误Fatal error: Cannot use object of type stdClass as array

那么可能是什么解决方案,如果我为 select 写了错误的查询,请纠正我

我想要“从 id = 12 的 customorders 中选择 *”,而 customorders 是我在 Drupal 数据库中创建的自定义表

请帮我..

谢谢

4

2 回答 2

2

$result变量作为对象数组返回。所以你应该使用$result->name而不是$result['name'].

您的代码可以固定为:

 $member_id = 12;

 $results = db_query("select * from {customorders} where id = :fid", array(':fid' => $member_id));
 foreach($results as $result) {
       $name = $result->name; // THE EDITED LINE.
 }

希望这行得通……穆罕默德。

于 2012-09-05T06:50:41.503 回答
0

这对我有用:

$member_id = 12;

 $results = db_query("select * from {customorders} where id = :fid", array(':fid' => $member_id))->fetchAll();
 foreach($results as $result) {
       $name = $result->name; // THE EDITED LINE.
 }

注意fetchAll()导致实际数据的结果,没有这个我只得到数据库对象。

于 2019-08-04T08:09:20.863 回答