msql查詢中報錯 Unknown column '黃色水果' in 'where clause'

守護大白菜發表於2017-05-03
<?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."'  就可以了











相關文章