致命错误:在行

时间:2017-11-02 12:08:14

标签: php mysql database phalcon

 class ABC extends \Phalcon\Mvc\Model
    {
        public $db;
        public function initialize()
        {
            $this->db=$this->getDi()->getShared('db');
        }
        public function getRows($role,$id)
        {
            $sql = "select *  from table where id=".$id;
            //echo $sql;  //exit;  
            //when i echo and execute the above query in db it is hsowing results fine.
            $connection = $this->db;
            $data       = $connection->prepare($sql);    //throwing error in this line 277 line
        }
    }       

我该如何调试此错误:

我有其他功能,对于他们数据库连接正在工作..!

  

致命错误:在非对象中调用成员函数prepare()   第277行/var/www/site/admin/app/models/modelname.php

这是我的迪:

 //Create a DI
    $di = new Phalcon\DI\FactoryDefault();

    $di->set('db', function() {

        $eventsManager = new \Phalcon\Events\Manager();

        //Listen all the database events
        /*  $eventsManager->attach('db', function($event, $connection) {
              if ($event->getType() == 'beforeQuery') {
                  echo '<div>'.$connection->getSQLStatement().'</div>';
              }
          });*/

        $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
            "host" => DBServer,
            "username" =>DBUsername,
            "password" => DBPwd,
            "dbname" =>DBName,
            "options" => array(
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
                PDO::MYSQL_ATTR_LOCAL_INFILE => true,
            )
        ));

        //Assign the eventsManager to the db adapter instance
        $connection->setEventsManager($eventsManager);
       return $connection;
    });

0 个答案:

没有答案