Horen
Horen

Reputation: 11382

$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?

Upvotes: 2

Views: 4292

Answers (3)

Aakash Sahai
Aakash Sahai

Reputation: 4004

May be late to answer, but I have the soultion.Thanks to @martynthewolf link.Two solution for zend I found:

$db->getConnection()->query($sql); // use getConnection()

$db->exec($sql);

This issue is because of memory stack size. On linux the stack grows as needed, but on Windows & Mac this issue gets bubbled because of the stack size. For this there a ticket raised in php.net(here) Have a look. Enjoy!!!

Upvotes: 1

a1ee9b
a1ee9b

Reputation: 31

I ran into the same error when I tried to insert a PDF into a BLOB. PHP's mysql_query() function worked fine, but the Zend functions query() or exec() just wouldn't work, although there were no error or exception thrown. This seems to be a problem known to Zend, since there are some bugs filed.

What worked for me is not the most elegant way to solve this, but it works. As mentioned above, PHP's mysql_query() worked, so I took the parameters from Zend application.ini and used them to create a new connection using PHP.

    <?php
    $front = Zend_Controller_Front::getInstance();
    $bootstrap = $front->getParam('bootstrap');
    $options = $bootstrap->getOptions();
    $dbOptions = $options["resources"]["db"]["params"];

    $link = mysql_connect($dbOptions["host"], $dbOptions["username"], $dbOptions["password"]);
    if (!$link) {
        die('Verbindung schlug fehl: ' . mysql_error());
    }
    mysql_select_db($dbOptions["dbname"]);

    $ret = (mysql_query($query) === TRUE)? TRUE : mysql_error();

    mysql_close($link);

    return $ret;
    ?>

This will either return TRUE or the mysql-error.

Upvotes: 2

drew010
drew010

Reputation: 69967

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.

Upvotes: 2

Related Questions