Search code examples
phpmysqlzend-frameworkzend-db

$adapter->query() in Zend not working - String to long for insert?


I am inserting data in Zend framework with the db adapter

$adapter = Zend_Db_Table::getDefaultAdapter();

and execute an insert statement like this:

$q = "INSERT INTO questions (category_id, user_id, `text`, active) 
VALUES($category_id, ".$user_id.", '".$question_text."', 1)";
$adapter->query($q);

However sometimes this query does not work. This only happens when $question_text is pretty long (>1000 chars) and does not depend on the content but only the length. Executing the same statement in phpmyadmin works without problems. (The db field is a text)

When I use

$adapter->exec($q);

the query works fine as well.

Now my questions:

What's causing the problem for the query() statement?

Are there any downsides to use exec() instead?


Solution

  • If you change the insert to use the object oriented interface which escapes your input data for you, do you still have the problem?

    $values = array('category_id' => $category_id,
                    'user_id'     => $user_id,
                    'text'        => $question_text,
                    'active'      => 1);
    
    $inserted = $adapter->insert('questions', $values);
    

    $inserted should contain the number of rows affected by the operation, 1.