PHP:为什么我不能在这个SQL查询中使用WHERE?

时间:2014-04-07 05:41:47

标签: php sql

我遇到了SQL查询问题。这是原始功能:

function selectAllSorted($active)
{
    return $this->selectObjects("SELECT bp.*, p.title as product_title
                                 FROM $this->_table bp 
                                 INNER JOIN ?_product p USING (product_id)
                                 ORDER BY 0+sort_order,p.title");
}

我修改它看起来像这样:

function selectAllSorted($active)
{
    return $this->selectObjects("SELECT bp.*, p.title as product_title
                                 FROM $this->_table bp
                                 WHERE product_id =$active
                                 INNER JOIN ?_product p USING (product_id)
                                 ORDER BY 0+sort_order,p.title");
}

但我得到一个未知错误,因为我无法看到日志,我无法确定它在哪里或为什么在这里使用WHERE是错误的。

这是定义函数selectObjects的方法:

function selectObjects($sql, $param1 = null)
    {
        $args = func_get_args();
        $q = call_user_func_array(array($this->_db, 'queryResultOnly'), $args);
        $ret = array();
        while ($row = $this->_db->fetchRow($q))
        {
            $obj = new $this->_recordClass($this);
            $obj->fromRow($row);
            $ret[] = $obj;
        }
        return $ret;
    }

你们可以弄清楚什么是错的?

谢谢。

2 个答案:

答案 0 :(得分:3)

您的语法错误 - where子句位于join子句之后:

function selectAllSorted($active)
{
    return $this->selectObjects("SELECT bp.*, p.title as product_title
                                 FROM $this->_table bp
                                 INNER JOIN ?_product p USING (product_id)
                                 WHERE bp.product_id =$active
                                 ORDER BY 0+sort_order,p.title");
}

答案 1 :(得分:0)

Join Query语法是:

Select t1.* from table1 t1
inner join table2 t2  on t1.col1=t2.col1
where t1.col2> 0 and t2.col3>4

现在,您的查询如下:

function selectAllSorted($active)
{
    return $this->selectObjects("SELECT bp.*, p.title as product_title
                                 FROM $this->_table bp
                                 INNER JOIN ?_product p on pb.product_id=p.product_id
                                 WHERE bp.product_id =$active                                
                                 ORDER BY 0+sort_order,p.title");
}