Location: PHPKode > scripts > Qexec > qexec/qexec.php
<?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 <hide@address.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 <hide@address.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($query, 0, 6)) != '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);
    }

}

?>
Return current item: Qexec