Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1
This is the forum to discuss the Wrox book Professional PHP5 by Ed Lecky-Thompson, Heow Eide-Goodman, Steven D. Nowicki, Alec Cove; ISBN: 9780764572821
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 10th, 2007, 11:51 PM
Registered User
 
Join Date: Dec 2007
Location: , , .
Posts: 3
Thanks: 0
Thanked 2 Times in 2 Posts
Default MySQL conversions - ch8 - Database Abstraction

class.Database.php

<?php
require_once('config_db.php');

class Database {

  private $debug = false;

  private $hConn;

  public function __construct() {

    global $cfg; // allow our method to access the $cfg associative array
              // by making it global

    $connString = ' host=' . $cfg['db']['host'];
    $connString .= ' user=' . $cfg['db']['user'];
    $connString .= ' password=' . $cfg['db']['password'];
    $connString .= ' port=' . $cfg['db']['port'];
    $connString .= ' dbname=' . $cfg['db']['name'];

  //debug
    if($this->debug){
        echo "<h3>".$connString."</h3>";
    }

// $this->hConn = mysql_connect($connString);
    $this->hConn = mysql_connect($cfg['db']['host'],$cfg['db']['user'],$cfg['db']['password']);

    if(! is_resource($this->hConn)) {
// throw new Exception("Unable to connect to the database " .
// "using \"$connString\"", E_USER_ERROR);
        throw new Exception("Unable to connect to the database") ;
    }

    //select db
    mysql_select_db($cfg['db']['name'], $this->hConn);
  }

  public function select($sql) {

   $hRes = mysql_query($sql , $this->hConn);
    if(!$hRes) {
        throw new Exception("select function error!");
    }

    $arReturn = array();
    while( ($row = mysql_fetch_assoc($hRes)) ) {
      $arReturn[] = $row;
    }

    return $arReturn;

  }

  public function update($table, $arFieldValues, $arConditions) {

    // create a useful array for the SET clause
    $arUpdates = array();
    foreach($arFieldValues as $field => $val) {
      if(! is_numeric($val)) {
        //make sure the values are properly escaped
        $val = "'" . mysql_escape_string($val) . "'";
      }

      $arUpdates[] = "$field = $val";
    }

    // create a useful array for the WHERE clause
    $arWhere = array();
    foreach($arConditions as $field => $val) {
      if(! is_numeric($val)) {
        //make sure the values are properly escaped
        $val = "'" . mysql_escape_string($val) . "'";
      }

      $arWhere[] = "$field = $val";
    }

    $sql = "UPDATE $table SET ";
    $sql .= join(', ', $arUpdates);
    $sql .= ' WHERE ' . join(' AND ', $arWhere);

   //debug
    if($this->debug){
        echo "<h3>".$sql."</h3>";
    }

    if (!mysql_query($sql, $this->hConn))
      {
          throw new Exception("update function error!");
      }
    return mysql_affected_rows($this->hConn);
  }

  function delete($table, $arConditions) {

    //create a useful array for generating the WHERE clause
    $arWhere = array();
    foreach($arConditions as $field => $val) {
      if(! is_numeric($val)) {
        //make sure the values are properly escaped
        $val = "'" . mysql_escape_string($val) . "'";
      }

      $arWhere[] = "$field = $val";
    }

    $sql = "DELETE FROM $table WHERE " . join(' AND ', $arWhere);

    //debug
    if($this->debug){
        echo "<h3>".$sql."</h3>";
    }

    if (!mysql_query($sql, $this->hConn))
      {
          throw new Exception("delete function error!");
      }

    return mysql_affected_rows($this->hConn);
  }



  public function insert($table, $arFieldValues) {

    $fields = array_keys($arFieldValues);
    $values = array_values($arFieldValues);

    // Create a useful array of values
    // that will be imploded to be the
    // VALUES clause of the insert statement.
    // Run the mysql_escape_string function on those
    // values that are something other than numeric.
    $escVals = array();
    foreach($values as $val) {
      if(! is_numeric($val)) {
        //make sure the values are properly escaped
// $val = "'" . mysql_escape_string($val) . "'";
        $val = "'" . mysql_escape_string($val) . "'";
      }
      $escVals[] = $val;
    }

    //generate the SQL statement
    $sql = " INSERT INTO $table (";
    $sql .= join(', ', $fields);
    $sql .= ') VALUES(';
    $sql .= join(', ', $escVals);
    $sql .= ')';
    $sql .= ';';

    //debug
    if($this->debug){
        echo "<h3>".$sql."</h3>";
    }

    if (!mysql_query($sql, $this->hConn))
      {
          throw new Exception("insert function error!");
      }

    return mysql_affected_rows($this->hConn);

  }



  public function __destruct() {
    if(is_resource($this->hConn)) {
      mysql_close($this->hConn);
    }
  }

}
The Following User Says Thank You to binne For This Useful Post:
shegxzyl (August 24th, 2009)
  #2 (permalink)  
Old December 10th, 2007, 11:56 PM
Registered User
 
Join Date: Dec 2007
Location: , , .
Posts: 3
Thanks: 0
Thanked 2 Times in 2 Posts
Default

If you use MySQL
please share you sample about this book
thanks ^^

--------------------------------------------
mytable.sql

create table mytable
 (id smallint unsigned not null auto_increment,
  myval varchar(255),
  constraint pk_mytable primary key (id)
 );


The Following User Says Thank You to binne For This Useful Post:
shegxzyl (August 24th, 2009)
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL conversions - chapter 7 gorillapoop BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 2 February 15th, 2011 11:02 AM
Database abstraction layer - OOP style w. paginati raul.ionescu PHP Databases 4 May 26th, 2006 03:19 PM
GenericObject with Database Abstraction soccer022483 BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 1 March 28th, 2006 07:09 AM
Database Abstraction Layer browngb BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 4 April 30th, 2005 03:34 PM
Type Conversions nikotromus Access VBA 2 October 7th, 2004 05:30 PM



All times are GMT -4. The time now is 10:35 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.