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: 296 All time: 7,429 This week: 125Up
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: 347242 in Mexico Mexico
Week rank: 587 Up9 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();


  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:296
This week:0
All time:7,429
This week:125Up