Connecting to a database
After seeing all the database types that Zend Framework 2 supports, we can finally start connecting to them. In this recipe, we will connect to a MySQL server and show different ways of doing this.
Getting ready
To make full of the following recipe, a Zend Framework 2 skeleton application should be used, with a MySQL server available to connect to. Don't forget that connecting to a MySQL server requires the mysql
and mysqli
extensions enabled in PHP.
How to do it…
In this recipe we'll give some examples of how to connect to a single database or multiple databases.
Connecting to a MySQL database through the configuration
We can make the following change to the /config/autoload/global.php
file:
<?php return array( // Set up the service manager 'service_manager' => array( // Initiate the connection at the start of the // application 'factories' => array( // Use the service factory to start up our db // adapter 'Zend\Db\Adapter...