PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of JImmy Bo   Bobo PHP DB Class   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: PHP Simple Database Class Usage Example
Class: Bobo PHP DB Class
Simple MySQL database class
Author: By
Last change: class example - verbose
Date: 7 years ago
Size: 2,126 bytes
 

Contents

Class file image Download
<?php
   
include_once('inc.config.php');
   
// if you like this class please rate it on
    // http://www.phpclasses.org/package/5283-PHP-Simple-MySQL-database-access-wrapper.html#ratings
   
    // not necessary but I put it in so you know it exists
   
$db->set_contype(0); # 0 = default (regular), 1 = peristent, 2 = persistent with compression, 3 = regular with compression
   
   
    // $db->q // insert or update
   
$sql = "INSERT INTO tablename (name, email) VALUES ('bob', 'user@email.com') ";
   
$id = $db->q($sql);
   
   
   
   
// $db->row // fetch a single row
   
$sql = "SELECT name, email FROM tablename";
   
$data = $db->r($sql);
   
# $data["name"] would equal 'bob'
    # $data["email"] would equal 'user@email.com'
   
   
   
    // $db->get // fetch an array
   
$sql = "SELECT name, email FROM tablename";
   
$rows = $db->get($sql);
   
# would return an array of multiple rows which you could traverse as such...
   
if(is_array($rows))
    {
        foreach(
$rows as $row)
        {
           
# $row["name"] would equal 'bob'
            # $row["email"] would equal 'user@email.com'
       
}
    }
   

   
# NEW!
   
    // $db->insert_from_array($table, $arr) // insert from an array
   
$table = 'tblorderpcs';
   
$arr = array (
           
"order_id" => $jobid,
           
"pcs_name" => $sname,
           
"pcs_qty" => $sqty,
           
"pcs_pn" => $spn,
           
"pcs_estimate" => $sest,
           
"pcs_memo" => $smemo,
           
"ship_id" => $ship_id,
           
"pcs_tstamp" => $time
       
);
   
$id = $db->insert_from_array($table, $arr);
   

   
// $db->update_from_array($table, $arr, $extra); // update from an array
   
$table = 'tblorderpcs';
   
$extra = "WHERE jID = $subjobid LIMIT 1";
   
$arr = array (
           
"pcs_name" => $sname,
           
"pcs_qty" => $sqty,
           
"pcs_pn" => $spn,
           
"pcs_estimate" => $sest,
           
"pcs_memo" => $smemo,
           
"ship_id" => $ship_id,
           
"pcs_made" => $smade,
           
"pcs_wasted"=> $swasted,
           
"pcs_ppu" => $sppu
       
);
       
    return
$db->update_from_array($table, $arr, $extra);

   
   
   
   
// you can force close the connection by calling. Not necessary
    // and if you perform another query after this it will automagically
    // reopen the connection.
   
$db->close();
?>