PHP Classes

File: example.php

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

 

Contents

Class file image Download
<?php
 /**
  * This is a basic 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
    $count = $db->query($sql);
    echo "there are $count records<br>";
    echo "<b>\$db->affected_rows</b> also return {$db->affected_rows} records<br>";
    
    #get all the records (by default as an array of object)
    print_r($db->get_all());
    
    #set new default: the next records will be return as an array of arrays
    $db->return_as_object(false);
    
    #get all records (as an array of arrays)
    print_r($db->get_all());
    
    #get the first record
    print_r($db->get_row());
    #get the second record (first=0, second=1, third=2, ...)
    print_r($db->get_row('',1));
    
    #get the 'username' column value of the second record
    print_r($db->get_value('',1,'username'));
    
    #finally, show posible errors...
    echo $db->getErrors();
?>