• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Zend Framework入门教程之Zend_Db数据库操作详解

php 搞代码 4年前 (2022-01-04) 33次浏览 已收录 0个评论

本文实例讲述了Zend Framework中Zend_Db数据库操作方法。分享给大家供大家参考,具体如下:

引言:Zend操作数据库通过Zend_Db_Adapter

它可以连接多种数据库,可以是DB2数据库、MySQli数据库、Oracle数据库。等等。

只需要配置相应的参数就可以了。

下面通过案例来展示一下其连接数据库的过程。

连接mysql数据库

代码:

<?phprequire_once 'Zend/Db.php';$params = array('host'=>'127.0.0.1',  'username'=>'root',  'password'=>'',  'dbname'=>'test'  );$db = Zend_Db::factory('PDO_Mysql',$params);

点评:

这是连接mysql的代码案例,提供相应的参数就可以了。连接不同的数据库,提供不同的参数。下面是sqlite的例子

代码:

<?phprequire_once 'Zend/Db.php';$params = array('dbname'=>'test.mdb');$db = Zend_Db::factory('PDO_Sqlite',$params);

点评:

sqlite明显参数不一样了,只需要提供数据库名字就可以了。
连接完数据库之后,就可以查询数据库信息以及操作数据库信息了。
如果查询呢?

下面是查询的代码案例:

<?phprequire_once 'Zend/Db.php';$params = array('host'=>'127.0.0.1',  'username'=>'root',  'password'=>'',  'dbname'=>'test'  );$db = Zend_Db::factory('PDO_Mysql',$params);$sql = $db->quoteInto('SELECT * FROM user WHERE id<?','5');$result = $db->query($sql);  //执行SQL查询$r_a = $result->fetchAll(); //返回结果数组print_r($r_a);

点评:

执行完上述代码,就会展示出数据库中前五条记录的信息。

那么这其中的玄机是什么呢?

我们来看一下源码。

我们来看看Db.php中的factory方法

public static function factory($adapter, $config = array()){    if ($config instanceof Zend_Config) {      $config = $config->toArray();    }    /*     * Convert Zend_Config argument to plain string     * adapter name and separate config object.     */    if ($adapter instanceof Zend_Config) {      if (isset($adapter->params)) {        $config = $adapter->params->toArray();      }      if (isset($adapter->adapter)) {        $adapter = (string) $adapter->adapter;      } else {        $adapter = null;      }    }    /*     * Verify that adapter parameters are in an array.     */    if (!is_array($config)) {      /**       * @see Zend_Db_Exception       */      require_once 'Zend/Db/Exception.php';      throw new Zend_Db_Exception('Adapter parameters must be in an array or a Zend_Config object'); <strong style="color:transparent">来源gao@daima#com搞(%代@#码网</strong>   }    /*     * Verify that an adapter name has been specified.     */    if (!is_string($adapter) || empty($adapter)) {      /**       * @see Zend_Db_Exception       */      require_once 'Zend/Db/Exception.php';      throw new Zend_Db_Exception('Adapter name must be specified in a string');    }    /*     * Form full adapter class name     */    $adapterNamespace = 'Zend_Db_Adapter';    if (isset($config['adapterNamespace'])) {      if ($config['adapterNamespace'] != '') {        $adapterNamespace = $config['adapterNamespace'];      }      unset($config['adapterNamespace']);    }    // Adapter no longer normalized- see http://framework.zend.com/issues/browse/ZF-5606    $adapterName = $adapterNamespace . '_';    $adapterName .= str_replace(' ', '_', ucwords(str_replace('_', ' ', strtolower($adapter))));    print_r($adapterName);exit;    /*     * Load the adapter class. This throws an exception     * if the specified class cannot be loaded.     */    if (!class_exists($adapterName)) {      require_once 'Zend/Loader.php';      Zend_Loader::loadClass($adapterName);    }    /*     * Create an instance of the adapter class.     * Pass the config to the adapter class constructor.     */    $dbAdapter = new $adapterName($config);    /*     * Verify that the object created is a descendent of the abstract adapter type.     */    if (! $dbAdapter instanceof Zend_Db_Adapter_Abstract) {      /**       * @see Zend_Db_Exception       */      require_once 'Zend/Db/Exception.php';      throw new Zend_Db_Exception("Adapter class '$adapterName' does not extend Zend_Db_Adapter_Abstract");    }    return $dbAdapter;}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Zend Framework入门教程之Zend_Db数据库操作详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址