Php 条令查询Where子句产生未知列错误
我有以下疑问:Php 条令查询Where子句产生未知列错误,php,doctrine,Php,Doctrine,我有以下疑问: return Doctrine_Query::create() ->from('Model_Article m') ->where($where) ->orderBy($order); 在$where中,我有: $where='m.title='.$name; 上述操作会产生此错误: Message: SQLSTATE[42S22]: Column not found: 1054 Unknown colu
return Doctrine_Query::create()
->from('Model_Article m')
->where($where)
->orderBy($order);
在$where
中,我有:
$where='m.title='.$name;
上述操作会产生此错误:
Message: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sadsa' in 'where clause'. Failing Query: "SELECT COUNT(*) AS num_results FROM article a WHERE a.title = sadsa"
为什么?错误是因为生成的查询看起来像“**其中title=sadsda**”,所以SQL引擎查找名为“sadsda”的列。为了避免这种情况,您必须提到,您希望与字符串进行比较,而不是与列值进行比较。您可以使用您的引擎规则(通常用“')”括起字符串),但它不安全,我认为您应该使用占位符引擎,例如,原则提供的占位符
$whereKey = 'm.title=?';
$whereValue = 'sadsda';
Doctrine_Query::create()->from('Model_Article m')->where($whereKey,$whereValue)->orderBy($order);
我认为您的查询应该是这样的:“选择COUNT(*)作为文章a的num_结果,其中a.title='sadsa'”