PHP Classes

File: example-advanced.php

Recommend this page to a friend!
  Packages of marcelo entraigas   Simple DB   example-advanced.php   Download  
File: example-advanced.php
Role: Example script
Content type: text/plain
Description: advanced example
Class: Simple DB
Store and retrieve data from a MySQL database
Author: By
Last change:
Date: 17 years ago
Size: 1,448 bytes
 

 

Contents

Class file image Download
<?php
 /**
  * This is an advanced example of using the Database class
  */

    # default database configuration
    $cfg_resource['default'] = array(
        'server'   => 'localhost',
        'database' => 'tradeaxes',
        'user'     => 'tradeaxes',
        'pass'     => 'tradeaxes',
    );

    #main includes
    require_once('config/lib/class.Error.php');
    require_once('config/lib/class.Database.php');

    #create a db object
    $db = new Database();
    
    #select all records from users
    $sql = "SELECT * FROM users";
    
    #execute the sql query and get the all records (by default as an array of object)
    print_r($db->get_all($sql));
    
    #get the 'username' column of all cached records
    print_r($db->get_all('','username'));
    
    #get the 'username' column of all cached records with the 'user_id' value as key
    print_r($db->get_all('','username', 'user_id'));
    
    #insert a new record
    $count = $db->query("INSERT INTO users (username,password,user_type) VALUES ('zippo', 'mynewpass','admin')");
    echo "there are $count record inserted<br>";
    echo "<b>\$db->affected_rows</b> also return {$db->affected_rows} record<br>";
    echo "<b>\$db->insert_id</b> return the last user_id: {$db->insert_id}<br>";
    
    
    #get the metadata associated with the sql query
    print_r($db->get_metadata());
    
    #get the enum values of 'use_type' column
    print_r($db->get_enum_values('users','user_type'));
    
    //show posible errors...
    echo $db->getErrors();
?>