A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

What is the safest way to do database queries in Zend Framework?

I have no idea. You tell me. All I do is this:

$sql = "SELECT * FROM Table1 WHERE id='".$target_id."'";  /*That is $target_id inside single-quotes in the SQL Query.*/

Please share your thoughts below.

This entry was posted in General and tagged , . Bookmark the permalink.

9 Responses to What is the safest way to do database queries in Zend Framework?

  1. Roman Nestertsov says:

    I prefer to use following thing:
    $sql = $db->quoteInto(“SELECT * FROM Table1 WHERE id = ?”, $target_id);

    Btw, source where you can read about quoting in Zend_Db: http://framework.zend.com/manual/en/zend.db.html#zend.db.adapter.quoting

  2. Wenbert says:

    Thanks Roman, appreciate the reply.
    just to clarify,
    $db = Zend_Registry::get(‘dbAdapter’);
    $sql = $db->quoteInto(”SELECT * FROM Table1 WHERE id = ?”, $target_id);
    is that correct?
    what about if i had 2 conditions in the WHERE part of the query? how would i do that using the quoteInto()?

  3. Roman Nestertsov says:

    If you need several conditions to quote you can use following schema:
    $sql = ‘SELECT * FROM Table1 WHERE id = ‘.$db->quote($target_id).’ AND param2 = ‘.$db->quote($param2);

    Read the link that i wrote above, all this things are described there

  4. Wenbert says:

    Awesome. Thanks a lot Roman :-)

  5. Erik says:

    You can also build the query using Zend_Db_Select

    $select = $db->select()->
    from(‘Table1′, ‘*’)->
    where(‘id = ?’, $id)->
    where(‘url = ?’, $url);

    $row = $db->fetchRow($select);

  6. Wenbert says:

    Ah nice!
    I have read about the query builder, but somehow, i never got the chance to use it because i couldn’t understand it on the ZF manual.

    It is time-consuming for me to make a long query and convert it using the query builder. I have always been comfortable with the more traditional sql queries – sadly i grew up with PhpMyAdmin and sometimes, i do queries with it – then copy-paste-edit the query.

    so a more complete example of the query builder would be something like this?

    $db = Zend_Registry::get(’dbAdapter’);

    $select = $db->select()->
    from(’Table1′, ‘*’)->
    where(’id = ?’, $id)->
    where(’url = ?’, $url);

    $row = $db->fetchRow($select);

    Thanks for all of your responses guys. Really appreciate all of them.

  7. Erik says:

    > sadly i grew up with PhpMyAdmin and sometimes,
    > i do queries with it – then copy-paste-edit
    > the query.

    I do the same thing sometimes, only recently have I tried to convert my SQL queries to the query builder. :)

    > so a more complete example of the query
    > builder would be something like this?

    Yeah, that works fine in my tests.

  8. Roman Nestertsov says:

    Query builder is a good solution if you a planning to use different databases. In this case Zend_Db class takes care about different notations in the SQL syntax depending on choosing database.

  9. Wenbert says:

    > I do the same thing sometimes, only recently
    > have I tried to convert my SQL queries to the
    > query builder.

    hehe it’s good know i’m not alone ;-)

    > Zend_Db class takes care about different
    > notations in the SQL syntax depending on
    > choosing database

    What about if my queries had “variables” and “conditions” in the SELECT part.
    Like: @ekini:=COALESC(t.field1,t.field2)
    Would that be possible in the query? (I’m not sure if i got that right, and I’m not if it only is possible in MySQL.)

    I’m going to read more on the query builder to find out what limitations do I get compared to not using it.

    And I’m assuming that the query builder is slower? And is just safer than making queries yourself in case you need to switch databases?

Leave a Reply to Erik Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>