Location: PHPKode > scripts > SQL Abstraction Layer > sqlal.php
<?php
/*
SQL Abstraction Layer Version 0.1

This program was created by John Kelesidis (hide@address.com)
Check http://www.jkelesidis.com for more information

Copyright (C) 2010  John Kelesidis

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    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, see <http://www.gnu.org/licenses/>.
*/
class SQLal
{
	private $DBServer;
	private $DBUser;
	private $DBPassword;
	private $DBName;

	function  __construct($DBServer, $DBUser, $DBPassword, $DBName)
	{
		$this->DBServer = $DBServer;
		$this->DBUser = $DBUser;
		$this->DBPassword = $DBPassword;
		$this->DBName = $DBName;
	}
	
	public function runsql($query)
	{
		$res = new SQLalResult();
		$error = false;
		if(mysql_connect($this->DBServer, $this->DBUser, $this->DBPassword))
		{
			if(mysql_select_db($this->DBName))
			{	
				$result = mysql_query($query);
				if($result)
				{
					$num = mysql_num_rows($result);
					$res->rowCount = $num;
					$res->sql_result = $result;	
					$fieldCount = mysql_num_fields($result);	
					if($fieldCount>0)$ftable = mysql_field_table($result,0);
						else $ftable = "";
					$onetable = 1;
					for($i=0;$i<$fieldCount;$i++)
					{
						if(mysql_field_table($result,$i)!=$ftable)$onetable = 0;
					}

					for($i=0;$i<$num;$i++)
					{	$pobj = null;
						
						for($j=0;$j<$fieldCount;$j++)
						{	$pr;
							$fieldName = mysql_field_name($result,$j);
							$fieldTable = mysql_field_table($result,$j);
							$cobj = (object) array ($fieldName=>mysql_result($result,$i,$fieldName));
							if($onetable==true)
								$pobj = (object) array_merge((array) $pobj , (array) $cobj);
							else 
							{
								$obj = (object) array (mysql_field_table($result,$j)=>$cobj);
								
								if($pobj->$fieldTable!=null)
									$pobj->$fieldTable = (object) array_merge((array) $pobj->$fieldTable , (array) $cobj);
								else
									$pobj = (object) array_merge((array) $pobj, (array) $obj);

							}
						
						}
						
						$res->row[$i] = $pobj;
					}
					return $res;
				}else $error = true;			
			}
			else $error = true;
		}
		else $error = true;
		if($error==true)
		{	
			$res->error = true;
			$res->errorMsg = mysql_error();
			$res->errorNumber = mysql_errno();
		}
		return $res;
	}
}

class SQLalResult
{
	public $error = 0;
	public $errorNumber = 0;
	public $multipleTables = 0;
	public $errorMsg = "No error";
	public $rowCount;
	public $row = array();
	public $sql_result;
}
?>
Return current item: SQL Abstraction Layer