Using Zend_Db Database Connection

I am trying to teach myself the Zend Framework. I have extensive experience using custom frameworks, but I have never used Zend. It is like trying to use a knife and fork with mittens.

My system is up and running. A database connection is created in the applition.ini file without errors.

The point I was in is trying to use this database connection to execute basic SQL. The .ini application has the lines:

db.adapter      = PDO_MYSQL
db.params.host  = localhost
db.params.username = cpanel_dbuser
db.params.password = 123456
db.params.dbname   = cpanel_dbname

I am trying to connect to the database in my /public/index.php

$config = new Zend_Config_Ini(APPLICATION_PATH . 
                              '/configs/application.ini', 'production');
$application->db = Zend_Db::factory($config->db);
Zend_Db_Table::setDefaultAdapter($db);

The error I get is:

Fatal error:  Uncaught exception 'Zend_Db_Exception' with message 'Adapter name must be specified in a string' in /home/path/library/Zend/Db.php:226
Stack trace:
#0 /home/path/public/index.php(32): Zend_Db::factory(Array)
#1 {main} thrown in /home/path/library/Zend/Db.php on line 226

And if I type r, the Config object

Zend_Config Object
(
    [_allowModifications:protected] => 
    [_index:protected] => 0
    [_count:protected] => 2
    [_data:protected] => Array
        (
            [adapter] => PDO_MYSQL
 /* ... more stuff ... */

From my understanding of the tutorials and PDFs that I'm working on, if I get this connection, I will be able to make such fantastic amazements as from inside the indexAction controller

 $data = $this->db->fetchAll(‘SELECT * FROM table’);
 foreach ($data as $row) {
      echo $row[‘table_fieldname’];
 }

, .

, , .

- ( , )?

+3
3

application.ini config do

resources.db.adapter = PDO_MYSQL
resources.db.isDefaultAdapter = true
resources.db.params.host = localhost
resources.db.params.username = username
resources.db.params.password = pwd
resources.db.params.dbname = mydatabase

, $db,

$db = Zend_Db_Table::getDefaultAdapter()

.

+10
public function ActionnameAction()
{
    $params=array(
            'host' =>'localhost',
            'username' =>'username',
            'password' =>'password',
            'dbname' =>'database_name');

    $db = Zend_Db::Factory('PDO_MYSQL',$params);
    $sql="select * from table_name";
    $result=$db->fetchAll($sql);
 }
+2

If I'm not mistaken, the adapters are case sensitive. You must use Pdo_Mysql. The exception you get does not seem to be related to this.

0
source

Source: https://habr.com/ru/post/1793453/


All Articles