PHP tutorial Part 1

Almost all PHP application requires a database. So I'm a Zend Framework fan. Let me show you how you can make use of Zend Framework data base classes what you need to make use of them. As we will only be using Zend Framework database classes therefore we don't need to download the complete framework. Let just download what we need to get started.

So to download only Zend Framework Database classes we need composer. The composer.json file we need will looks like below:

    "require": {
        "php": ">=5.5",
        "zendframework/zend-db" : "~2.5"

After this we need to run composer from command line and the command is:

php composer.phar install
After that create a php file with name zend-db-test.php and put in the code to run your db related task:

// Setup autoloading
require 'vendor/autoload.php';

use Zend\Db\ResultSet\ResultSet;

$adapter = new Zend\Db\Adapter\Adapter(array(
    'driver' => 'Mysqli',
    'database' => 'yourdatabase',
    'username' => 'UserName',
    'password' => 'UserPassword',
    // below is required for Mysqli Adapter of Zend Framework
    'options' => array(
         'buffer_results' => true,
$stmt = $adapter->createStatement('SELECT * FROM `user` WHERE `user_id` = ?');
$result = $stmt->execute(array(1));
$resultSet = new ResultSet;

print_r($resultSet->toArray() );

// Building up Query using Select object

use Zend\Db\Sql\Sql;
$sql = new Sql($adapter, 'user');
$select = $sql->select();
$select->where(array('user_id' => 1));
$selectString = $sql->getSqlStringForSqlObject($select);
$results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);

$resultSet = new ResultSet;

foreach ($resultSet as $row)
	echo '<br />'. $row->user_id . '::' . $row->email;