20

Is there a way to execute a SQL String as a query in Zend Framework?

I have a string like that:

$sql = "SELECT * FROM testTable WHERE myColumn = 5"

now I want to execute this string directly withput parsing it and creating a Zend_Db_Table_Select object from it "by hand". Or if thats possible create a Zend_Db_Table_Select object from this string, to execute that object.

How can I do that? I didn't find a solution for this in the Zend doc.

4 Answers 4

28

If you're creating a Zend_DB object at the start you can create a query using that. Have a look at this entry in the manual : https://framework.zend.com/manual/1.12/en/zend.db.statement.html

$stmt = $db->query(
            'SELECT * FROM bugs WHERE reported_by = ? AND bug_status = ?',
            array('goofy', 'FIXED')
        );

Or

$sql = 'SELECT * FROM bugs WHERE reported_by = ? AND bug_status = ?';
$stmt = new Zend_Db_Statement_Mysqli($db, $sql);
$stmt->execute(array('goofy', 'FIXED'));
Sign up to request clarification or add additional context in comments.

4 Comments

does $db->query($sql, array()) work also? because I do have the parameters already qouted into the string!? And do I have to fetch the resultSet with $stmt->fetchAll() ?
For the sake of completeness: yes, $stmt->fetchAll() will get the results.
Most of the times, it's better to use the framework since there a lot of automatic protections. But sometimes you want to optimize a query or get some very specific data. Those times there are no ways around writing custom queries. Just make sure to sanitize your query properly.
I declare $db =Zend_Db_Table_Abstract::getDefaultAdapter(); inside my model's function
5

If you are using tableGateway, you can run your raw SQL query using this statement,

$this->tableGateway->getAdapter()->driver->getConnection()->execute($sql);

where $sql pertains to your raw query. This can be useful for queries that do not have native ZF2 counterpart like TRUNCATE / INSERT SELECT statements.

Comments

3

You can use the same query in Zend format as

$select = db->select()->from(array('t' => 'testTable'))
                     ->$where= $this->getAdapter()->quoteInto('myColumn = ?', $s);
$stmt = $select->query();
$result = $stmt->fetchAll();

1 Comment

You don't need to use quoteInto() here. where() will do it for you automatically.
3

Here is an example for ZF1:

$db =Zend_Db_Table_Abstract::getDefaultAdapter();
$sql =    "select * from user"
$stmt = $db->query($sql);
$users =  $stmt->fetchAll();

4 Comments

for me it doesn't work, btw where I have to define $db variable mention above, in my class that extends from Zend_Db_Table_Abstract or in my controller?
@adm you can use $db =Zend_Db_Table_Abstract::getDefaultAdapter(); anywhrere. The above code is from my controller.
It gives me an error like this : Fatal error: Call to a member function query() on a non-object in /home/tmtmedia/public_html/trakmysafari/application/models/trip.php on line 199
you can use the above code in controller. If you have a class (e.g. MyModel) that extends with Zend_Db_Table_Abstract then try to check all the public functions using "get_class_methods" and if you find getDefaultAdapter() then you can use this code in the class (MyModel): $rows = $this->getDefaultAdapter()->query("select * from mytable")->fetchAll();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.