A Long time ago, i wrote a Class, that makes
৺SQL-Querys easier:
<?php
class SQL{
var $Result;
var $Server = "localhost";
var $User = "user";
var $Password = "password";
var $Database = "general";
var $LastInsertId = 0;
var $LastErrorMsg = "";
function SQL($Database=""){
if ($Database) $this->Database = $Database;
}
function Query($Query){
$db_conn = @mysql_connect($this->Server,$this->User,$this->Password);
}
function GetResultRow($Mode = MYSQL_ASSOC){
}
function GetLastInsertId(){
return $this->LastInsertId;
}
function Status(){
if ($this->LastErrorMsg["Status"] != 0) return false;
else return true;
}
function GetLastErrorId(){
return $this->LastErrorMsg["Status"];
}
function GetLastErrorMsg(){
return $this->LastErrorMsg["Msg"];
}
function GetLastErrorQuery(){
return $this->LastErrorMsg["Query"];
}
function GetCount(){
}
}
?>
The usage is very Simple:
First, there is the Constructor. If "no" Value passed, the defined
৺Database will be used - else the one specified will be used to establish the connection:
$mySQL = new SQL("DataBaseName");
Now you can build your query in a string -or simple use the command direct:
$mySQL->Query("SELECT column1,column2 FROM table ORDER BY column1");
To loop through the result, you simple can use the line:
While ($Row = $mySQL->GetResultRow($Option)){
//Do Some Stuff with lines
}
Where Option is
MYSQL_NUM or
MYSQL_ASSOC (default).
To make error handling possible, i added the functions, explained bellow:
returns
true if everything is fine, and
false if something has gone wrong...
To gather information about what's going wrong, you can use
$mySQL->GetLastErrorMsg();
$mySQL->GetLastErrorId();
$mySQL->GetLastErrorQuery();
ErrorMsg will be the error string as known from
৺MySQL.
ErrorId will be the related number.
ErrorQuery will return the query, that causes this error. (For logging, debuging, ...)
returns the number of rows, the select query has matched.
$mySQL->GetLastInsertId();
returns the id, associated with the Entry, the last insert query has created.