开发者

"Syntax error" when using Zend_Db to query MySQL database

开发者 https://www.devze.com 2023-03-01 01:30 出处:网络
This is my query: $query = $db ->select() ->from(array(\'ns\' => \'news_subscriber\'), array(\'ns.id\', \'ns.subscriber_email\')

This is my query:

$query = $db
    ->select()
    ->from(array('ns' => 'news_subscriber'),
        array('ns.id', 'ns.subscriber_email')
    )
    ->where('ns.id NOT IN (?)', 
        $db
            ->select()
            ->from(array('nss'开发者_高级运维 => 'news_subscribers_has_news_letter_content'),
                array('nss.news_subscribers_id')
            )
            ->where('nss.news_letter_content_id =' , $id)
    );
$subscribers = $db->fetchAll($query);

I am getting this error:

Syntax error or access violation 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '))))' at line 1`

I feel the problem is in the "IN".

Any ideas?


Your query has multiple errors.

  • ->where('nss.news_letter_content_id =', $id)

    You forgot the ? after the =.

  • ->where('ns.id NOT IN (?)', $db->select() ...

    I'm pretty sure that you have to convert the subquery object to an array first.


$subscribers = $db->fetchAll($db->select()->from('news_subscriber ns, subscriber_email se')
->where('ns.id NOT IN ('.$db->select()->from( 'news_subscribers_has_news_letter_content nss') ->where('nss.news_letter_content_id =',$id))));

I dont use array in my select. See if this is helpful.


you may write your query like this..please check below.

$subscribers = $db->fetchAll($db->select()->from(array('ns' => 'news_subscriber','nss'=> 'news_subscribers_has_news_letter_content'),
                                array('ns.id',
                                    'ns.subscriber_email','nss.news_subscribers_id'))                
                ->where('ns.id NOT IN (?) AND 'nss.news_letter_content_id =',$id);

Thanks.


Note that you can always see the whole query to see where is the problem.

echo (string)$query;
// die();


are you sure that all the variables that you have used in your query are getting a value, most of the times this error occurs if the values of the variables in the query are uninitialized or "" in case of a string ......... Also in this case I think you have an extra ')' in the end hope that helps

0

精彩评论

暂无评论...
验证码 换一张
取 消