Connection to MySQL database from Zend framework 1.12.3 in Netbeans

First create in MySQL server country_db database with only one table country. I do it by means of phpMyAdmin.
You write in empty field name of database and select utf8_unicode_ci as Collation and click Create button.
On the list of databases click on country_db link.
c70bIn Name field write name of table: country and in Number of column write 2.
Click on Go button.
Write name of column for this table: country, continent and id(as primary key with auto increment.
c70eSet data in the table.
This is list of countries in table country.
c70Then create project country.
Create in Netbeans project country. Choose from menu File->New Project.
c72Select PHP from Categories part and PHP Application from Projects part of window.
Click Next button. In Projects Name field write country. Click Next button.
c71In Project URL field add in path public word.
c73Click Next button.
Choose Zend PHP Web Framework from the list. Click Finish button.
c74In structureof tree You see country project’s nodes.
c75First You must configure connection to databse so open application.ini file from application/configs node and write these lines on the end file:

    resources.db.adapter = "pdo_mysql"
    resources.db.params.dbname = "country_db" = "localhost"
    resources.db.params.username = "root"
    resources.db.params.password = "dorota"
    resources.db.params.charset = "utf8"

First line it is only information in this file that it is part about MySQL configuration.
Another are params for database.
In this moment You may create layout for Your application. Right click on the project name and select from pop-up menu Zend->Zend Command.
c77In Filter field write enable layout and click Run button.
c78In Projects window double click on layout.phtml file.
c79In the file paste code:

    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <?php echo $this->layout()->content; ?>

and save file.
c80There You must generate class which will be connect to database. In Run Command window in Filter field write:
create db-tab
and in Parameters field write:
Country country
where Country is class’s name and country is table’s name.
c81Click Run command.
In model node You see DbTable node with Country.php file.
c82This file is class for connect to country table in database.
c83Model is used to logical operation, example for database operations.
Double click on IndexController.php file and write this code in indexAction method:
$c=new Application_Model_DbTable_Country();

c84Variable $c contain object class implemented in Country.php file.
For this object is executed method fetchAll getting all data from table country.
This data we set in view for controller index. This view is index view. So open file view/script/index/index.phtml.
Delete from this file content and write this code:

  <?php foreach ($this->rzeki as $rzeka): ?>
                <td><?php echo $c['name'] ?></td>
                <td><?php echo $c['continent'] ?></td>
        <?php endforeach; ?>

c85Here You run Your project.


Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.