<?php
$positionarr = ['1'=>'绿色蔬菜','2'=>'红色蔬菜','3'=>'黄色蔬菜', '4'=>'绿色水果', '5'=>'红色水果', '6'=>'黄色水果'];
$position_id = 6; //水果类型
$position_name = $positionarr[$position_id];
$sql = "select a.type,a.url,a.name,a.businessid,a.tourl,a.startdate,a.enddate,b.id,b.name as bname from
".MFruitBaseinfo::tableName()." as a inner join ".MFruitPositioninfo::tableName()." as b on a.id=b.adid
where a.status=0 and b.status=0 and b.name=".$position_name."
order by b.displayorder desc,b.id desc";
$result = MFruitBaseinfo::getDb()->createCommand($sql)->query();
$rows = $result->readAll();
print_r($rows);
?>
运行上述代码报错:[Err] 1054 - Unknown column '黄色水果' in 'where clause';
排错:
表中明明有该字段,,说明是sql语句有误,,,后来将 b.name=".$position_name." 改为b.name='".$position_name."' 就可以了