Zend Framework Configuration Operation database instance analysis _php skill

Source: Internet
Author: User
Tags php foreach php tutorial wordpress database zend zend framework

Zendframework project Environment, after looking at the next Zend Framework configuration operation database, PHP tutorial is as follows:
Create a Config.ini file under the Application/configs file
configuration information is as follows
[General]
Db.adapter=pdo_mysql
Db.config.host=localhost/iparess
Db.config.username=username
Db.config.password=password
Db.config.dbname=databasename
2,
In the index.php page of the PULIBC directory
/** zend_application * *
Require_once ' zend/application.php ';
Insert Below
Set the Datase config
Require_once ' zend/config/ini.php ';
Require_once ' zend/registry.php ';
Require_once ' zend/db.php ';
Require_once ' zend/db/table.php ';
$config =new Zend_config_ini ('./.. /application/configs/config.ini ', NULL, true);
Zend_registry::set (' config ', $config);
$dbAdapter =zend_db::factory ($config->general->db->adapter, $config->general->db->config-> ToArray ());
$dbAdapter->query (' SET NAMES UTF8 ');
Zend_db_table::setdefaultadapter ($dbAdapter);
Zend_registry::set (' Dbadapter ', $dbAdapter);
In this regard, I use my local wordpress database to test, use the Wp_posts table to test it:
First, model models establish wp_posts.php

Copy Code code as follows:

<?php
Class Wp_posts extends zend_db_table{
protected $_name = ' wp_posts ';
protected $_primary = ' ID ';
}
?>

Controller controller The following set up indexcontroller.php
Copy Code code as follows:

<?php
Require_once Application_path. ' /models/wp_posts.php ';
Class Indexcontroller extends Zend_controller_action
{
Public Function init ()
{
/* Initialize Action Controller Here * *
}
Public Function indexaction ()
{
$con = new Wp_posts ();
$res = $con->fetchall ()->toarray ();
$this->view->res = $res;
$this->render ("index");
}
}

To create a view in views/scripts/index/: index.phtml
Copy Code code as follows:

<title>this is for test</title>
<body>
<table>
<?php foreach ($this->res as $news) {?>
<tr>
<td><?php echo $news [' ID ']?></td>
<td><?php echo $news [' Post_title ']?></td>
<td><?php echo $news [' Post_date ']?> </td>
</tr>
<?php}?>
</table>
</body>

OK, the browser shows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.