Zend Framework and PDO_MYSQL

I’ve been working with the Zend Framework a bit and in working with MySQL through PDO_MYSQL. I’ve run into a few problems trying to use parameters of queries.

I’ve narrowed the issue down to PDO itself and not the Zend Framework.


$dbh = new PDO("mysql:host=localhost;dbname=db","user","pw");
$query = "insert into silo_test_data (record_id, fieldname, value)
values (3,'stufftest', :value )";
$handle = $dbh->prepare($query);
$handle->execute(array(":value" => 'crap'));
$dbh = null;

The value gets inserted into the database as an empty string, or sometimes some low-value bytes. I’ve managed to work around this temporarily thanks to some help from this post. By setting PDO to emulate prepared statements, everything seems to work okay.


$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);

The problem is, the Zend framework encapsulates the actual db connection and connects lazily. So, for now I’m running a query and then set the parameter on the encapsulated connection object.


$db_connect = array( 'host' => $config->db->hostname,
'username' => $config->db->username,
'password' => $config->db->password,
'dbname' => $config->db->database );
$db = Zend_Db::factory('PDO_MYSQL', $db_connect);
$db->query('select 1');//HACK HACK HACK HACK to initiate connection. Can't I make this a plugin? or extend the class?
$db->getConnection()->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
Zend::register('db',$db);

4 comments to Zend Framework and PDO_MYSQL

  • vem

    Hi,

    I tried your hack, but it works only for string, if I tried it with integer value, it doesn’t works.
    Can you help me how to make it?

    Thanks,
    vem

  • What part only works for a string? I’m a bit confused about your question.

  • vem

    this one:
    $handle->execute(array(“:value” => ‘crap’))

    I tried it in this way
    $handle->execute(array(“:value” => 15))
    and it doesn’t work for me

  • rakesh

    Hi,

    I have started learning ZF. I am using 1.8.
    I am not able to figure out how to call my stored functions or procedures from zend framework.
    tried some ways but its give error. here is code – decode_error is my stored function in mysql db.

    $config = new Zend_Config_Ini(APPLICATION_PATH.”/configs/application.ini”);
    $db = Zend_Db::factory($config->resources->db->adapter, array($config->resources->db->params->toArray()));
    $qu = $db->prepare(“select decode_error(?,?)”);
    $qu->execute(array(‘staging_eqb_horse’, $errorCode));
    $res = $qu->fetchAll();

    please help me out

    – rakesh

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>