PHP Classes
Icontem

File: qexec.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Giulio Bai  >  Qexec  >  qexec.php  
File: qexec.php
Role: Class source
Content type: text/plain
Description: Main class file, containing comments and examples
Class: Qexec
MySQL database access wrapper with many functions
 

Contents

Class file image Download
<?php

/**
 * Qexec - PHP Class to execute select queries safer and quicker, binding results
 *
 * Tested and working on PHP 4.3.2 and higher
 *
 * LICENSE: This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License v2 as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * @author       Giulio Bai <slide.wow@gmail.com>
 * @copyright   (C)2007 Giulio Bai - GNU GPL
 * @license       http://www.gnu.org/licenses/gpl.html GNU General public License
 * @version      1.0
 * @link            http://hewle.com/
 */
 

/**
 * A class to make queries safer and bindings quicker
 * 
 * Example of use (1):
 * The following code will returns affected_rows for anything other than select's.
 * If you don't want any field name keys then specify 0 as the second parameter.
 * <code>
 * include_once('qexec.php');
 *
 * $q = new Qexec();
 * $res = $q->exec_query('SELECT foo,bar FROM table');
 *
 * echo $res[0]['foo']; // First row of the field 'foo'
 * </code>
 *
 * Example of use (2):
 * For a single field select, returns the value
 * <code>
 * include_once('qexec.php');
 *
 * $q = new Qexec();
 * $count = $q->exec_query('SELECT count(*) FROM table');
 * </code>
 *
 * @author      Giulio Bai <slide.wow@gmail.com>
 * @copyright  (C)2007 Giulio Bai
 * @license      http://www.gnu.org/licenses/gpl.html GNU General Public License
 * @version     1.0
 * @link           http://hewle.com
 */
 
class Qexec
{

    
/**
     * In case of error, this is the error message
     *
     * @var string
     */
    
var $error 0;
    
    var 
$link mysql_connect('mysql_host''mysql_user''mysql_password'); // Enter your data here
    
    
    /**
     * Builds and returns the multiple array of results.
     *  Found on http://it2.php.net/manual/en/function.mysql-query.php and
     *  modified to match the class
     * 
     * @param string $query the SELECT query to execute
     * @param bool $assoc want the result to be assoc?
     *
     * @return mixed the results, in a multi-array
     */
    
function exec_query($query$assoc 1)
    {
       
$query parse_mysql_query($query);
       
       
$result get_result($query);
       
       if (
$this->error !== 0) die($this->error); // Query not executed, due to errors
       
       
if( strtolower(substr($query06)) != 'select' ) return array(mysql_affected_rows(), mysql_insert_id());
       
       
$count = @mysql_num_rows($result);
       
       if( !
$count ) return 0;
       
       if(
$count === 1) {
           if(
$assoc$f mysql_fetch_assoc($result);
           else 
$f mysql_fetch_row($result);
           
           
mysql_free_result($result);
           
           if(
count($f) === 1) {
               list(
$key) = array_keys($f); 
                 
               return 
$f[$key];
           } else {
               
$all = array();
               
$all[] = $f;
               
               return 
$all;
           }
       } else {
           
$all = array();
           
           for(
$i=0$i<$count$i++) {
               if( 
$assoc $f mysql_fetch_assoc($result);
               else 
$f mysql_fetch_row($r);
               
$all[] = $f;
           }
           
           
mysql_free_result($result);
           
           return 
$all;
       }
    }
    
    
    
/**
     * Actually executes the query and return the result
     *
     * @param string $query the SELECT query to execute
     * @param bool $assoc want the result to be assoc?
     *
     * @return mixed the results, in a multi-array
     */    
    
function get_result($query)
    {
        
$result = @mysql_query($query$this->link);
           
        if(
mysql_errno()) {
            
$this->error 'MYSQL ERROR #' mysql_errno() . ' : ' mysql_error() . '<br>' $query '';
            
            return 
0;
        }
        
        
$this->error 0;
        
        return 
$result;
    }

    
/**
     * AParses the query and connects to the db
     *
     * @param string $query the SELECT query to execute
     *
     * @return string the query, again, escaped
     */
    
function parse_mysql_query($query)
    {
        if(!
is_resource($this->link)) {
            
$this->error "Failed to connect to the database. Please check your data.";
            
            return 
0;
        }

        
$this->error 0;
        
        return 
mysql_real_escape_string($query);
    }

}

?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products