PHP Classes

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

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 53%Total: 302 All time: 7,397 This week: 36Up
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: 3 packages by
Country: Mexico Mexico
Age: ???
All time rank: 330937 in Mexico Mexico
Week rank: 215 Up4 in Mexico Mexico Up
Innovation award
Innovation award
Nominee: 1x

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 (3)  
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

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:302
This week:0
All time:7,397
This week:36Up
 User Ratings  
 
 All time
Utility:75%StarStarStarStar
Consistency:83%StarStarStarStarStar
Documentation:-
Examples:75%StarStarStarStar
Tests:-
Videos:-
Overall:53%StarStarStar
Rank:2225