PHP Classes

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    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 298 All time: 7,420 This week: 118Up
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.

Picture of Ezhaym Najera M
  Performance   Level  
Name: Ezhaym Najera M <contact>
Classes: 2 packages by
Country: Mexico Mexico
Age: ???
All time rank: 346842 in Mexico Mexico
Week rank: 368 Up2 in Mexico Mexico Up

Example

<?php

require_once 'DAO.php';

// Your own class
// Tu clase propia
class UserDao extends DAO
{
    public function
get_user(int $id)
    {
       
// If method is executeGet(), it will return result set from database
        // Si el método es executeGet(), devolverá los datos de la base de datos
       
$this->executeGet("SELECT * FROM users WHERE id = :id", ['id' => $id]);
    }
    public function
save_user($name)
    {
       
// Use execute() method to store data and set_msj() method to create custom message
        // Usar método execute() para insertar datos y el método set_msj() para crear un mensaje
       
$this->execute("INSERT INTO users (name) VALUES (:name)", ['name' => $name]);
       
$this->message("User saved successfully!");
    }
}

$user = new UserDao;

// Get user with id = 1
$user->get_user(1);

if (!
$user->result['error']) {
   
/* If correct, print name of user. Index 0 means get first element of array response
       Si no hay error imprimir el nombre del user
       El índice 0 indica que se obtiene el primer elemento del array de respuesta */
   
echo $user->result['data'][0]['name'];
}
else {
   
// If error, print error message
    // Si hay error imprimir el mensaje
   
echo $user->result['data'];
}

// Close MySQL connection
// Cierra la conexión a MySQL
$user->close();


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:298
This week:0
All time:7,420
This week:118Up