2

I'm looking to execute a raw SQL query in a Zend Controller.

I looked here http://framework.zend.com/manual/1.11/en/zend.db.statement.html and it says to do

    $sql = 'SELECT * FROM bugs WHERE reported_by = ? AND bug_status = ?';

    $stmt = new Zend_Db_Statement_Mysqli($db, $sql);

    $stmt->execute(array('goofy', 'FIXED'));

but whenever I do that it gives me an error cause of the $db.... What's $db supposed to hold cause it doesn't say anywhere in the documentation...

Also whenever I'm done I would like to return an array with whatever the result was, what would I be able to do something like $resultArray = $stmt->execute(array('goofy', 'FIXED')); ???

Thanks,

Brandy
  • 544
  • 2
  • 11
  • 30

2 Answers2

1

It should be an object of type Zend_Db_Adapter.

Elsewhere in the documentation examples, the variable $db is commonly used as an instance of a Zend_Db_Adapter. I guess one is expected to read the full documentation, and notice conventions like that.


Re your comment:

You can load your application.ini file into a Zend_Config_Ini object, and then use that Zend_Config object as the argument to Zend_Db_Factory. See example here: http://framework.zend.com/manual/1.11/en/zend.db.adapter.html#zend.db.adapter.connecting.factory-config

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • As of right now, all my Database info is being stored in my application.ini, is there a way to call the mysql info from that file to the Zend_Db_Adapter so I won't have to change it in 2 places if I was to change any of the info in the future? – Brandy Jul 15 '14 at 22:55
  • Awesome, I read your other RE comment, and that way looks a lot easier. One question though, it says to do ` /path/to/config.ini` but I'm not able to get to the application.ini. So am I going to have to create a new file in the public or is there a way to get to the application.ini file? – Brandy Jul 15 '14 at 23:08
  • @BrandonStewart Look at the following posts how to get the db adapter: http://stackoverflow.com/a/1786979/1948627, http://stackoverflow.com/a/925852/1948627 – bitWorking Jul 15 '14 at 23:11
0
Try this code:

Put code in bootstrap.php
protected function _initRegistry() {
        $this->bootstrap('db');
        $dbc = $this->getResource('db');
        $dbc->setFetchMode(Zend_Db::FETCH_OBJ);
        Zend_Registry::set('db', $dbc);
        global $db;
        $db = Zend_Registry::get('db');
    }
Indrajeet Singh
  • 2,958
  • 25
  • 25