PHP Classes
elePHPant
Icontem

DAO for PHP: Access data objects in a database using PDO

Recommend this page to a friend!
  Info   View files Example   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2019-02-11 (7 days ago) RSS 2.0 feedNot enough user ratingsTotal: 218 This week: 2All time: 8,001 This week: 222Up
Version License PHP version Categories
dao-for-php 1.0.3Freely Distributable7PHP 5, Databases
Description Author

This class can be use to access data objects in a database using PDO.

It provides a base class that should be extended by application data access classes to execute queries to store and retrieve objects in database table records.

The base class can establish database connections, execute queries and store error messages when the queries fail.

  Performance   Level  
Name: Ezhaym Najera M <contact>
Classes: 2 packages by
Country: Mexico Mexico

Details

Dao-for-PHP v1.0

Abstract class DAO for PDO/MySQL

Class heritage Example

class User extends DAO
{
    public function get_by_id(int $id)
    {
        $sql = 'SELECT * FROM users WHERE id = :id';
        $this->executeGet($sql, ['id' => $id]);
    }

    public function create_user(string $name)
    {
        $sql = 'INSERT INTO users (name) VALUES (:name)';
        $this->execute($sql, ['name' => $name]);

        $this->message('User created successfully!');
    }
}

Get user by id example

$user = new User();

$user->get_by_id(1);

if ($user->result['error']) {
    $error = $user->result['data'];
} else {
    $user_obtained = $user->result['data'][0];
}

Create user example

$user = new User();

$user->create_user('John Doe');

$response = $user->result['data'];

Properties:

public array $result

  • _$this->result['error']_ - Boolean
  • _$this->result['data']_ - Array|String from executeGet() method - String from execute() method

protected PDO $con - PDO MySQL connection

protected PDO $query - PDO prepared statement for execution

Methods:

public void execute(string $query, array $params)

public void executeGet(string $query, array $params)

  • _string $query_ - SQL sentence with alias
  • _array $params_ - Assoc array with alias as key

public void message(string $message)

  • _string $message_ - Create custom response message after execute() method

public void close()

  • Close MySQL connection
  Files folder image Files  
File Role Description
Plain text file DAO.php Class Class source
Accessible without login Plain text file demo.php Example Example script
Accessible without login Plain text file README.md Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:218
This week:2
All time:8,001
This week:222Up