我用过
$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
写了错误的查询,请纠正我我希望“select * from customorders where id = 12” 和自定义程序是我在Drupal数据库中创建的自定义表
请帮帮我..
由于
答案 0 :(得分: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.
}
希望这有效......穆罕默德。
答案 1 :(得分: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()
,没有这个我只能得到数据库对象。