I did this class-based MySQL connection script to avoid repeating PHP MySQL functions and keeping code clean.
It's probably full of bugs, but it worked for me.
To use it in a SELECT statement, I do:
$DB = new DataBase();
$DB->Query("SELECT * FROM Table");
if ($DB->QueryOK() && $DB->TotalRows()) {
while($row = $DB-getRow()) {
/* do stuff */
}
}
To use it in an INSERT statement, I do:
$DB = new DataBase();
$DB->Query("INSERT INTO Table(id, value) VALUES(1, 'blah');
if ($DB->QueryOK() && $DB->AffectedRows()) {
/* insert successfull */
}
Of course, you should use defines shown below:
define('BD_Server', 'localhost');
define('BD_Username', 'dbms_username');
define('BD_Password', 'dbms_password');
define('BD_Database', 'database_name');
Or call the function this way:
$DB = new DataBase('localhost', 'dbms_username', 'dbms_password', 'database_name');
class DataBase {
private $Conexion;
private $DB;
private $Query;
private $AffectedRows;
private $NumResult;
private $TotalRows;
private $ConexionOK;
private $QueryOK;
private $Error;
/*
* Class constructor.
*/
public function DataBase($Servidor = BD_Server, $Usuario = BD_Username, $Password = BD_Password, $NombreDB = BD_Database) {
$this->Query = false;
$this->ConexionOK = false;
$this->QueryOK = false;
$this->Error = "";
$this->AffectedRows = 0;
$this->NumResult = 0;
$this->TotalRows = 0;
$this->Conexion = @mysql_connect($Servidor, $Usuario, $Password);
if ($this->Conexion) { // Successfull connection.
$this->DB = @mysql_select_db($NombreDB, $this->Conexion);
$this->ConexionOK = ($this->DB) ? true : false;
}
else {
$this->ConexionOK = false;
}
}
/*
* Run queries.
*/
public function Query($SQL) {
$this->Error = "";
$this->AffectedRows = 0;
$this->NumResult = 0;
$this->TotalRows = 0;
$this->Query = @mysql_query($SQL, $this->Conexion) OR $this->Error = mysql_error($this->Conexion);
if (strlen($this->Error) > 0) $this->QueryOK = false;
else $this->QueryOK = true;
if ($this->QueryOK) {
$this->AffectedRows = @mysql_affected_rows($this->Conexion);
$this->NumResult = @mysql_num_rows($this->Query);
$this->TotalRows = $this->NumResult;
}
return ($this->QueryOK) ? $this->Query : NULL;
}
/*
* Returns true if the connection was established correctly.
*/
public function OK() {
return $this->ConexionOK;
}
/*
* Returns true if the last query was succesfully executed.
*/
public function QueryOK() {
return $this->QueryOK;
}
/*
* Returns mysql_error() string.
*/
public function Error() {
return $this->Error;
}
/*
* Gets one row from the MySQL query.
* Should use: if ($DB->QueryOK() && $DB->TotalRows()) $row = $DB->getRow();
*/
public function getRow() {
if ($this->ConexionOK && $this->QueryOK) { // If successfull query.
if ($this->NumResult > 0) { // If there still are rows which have not been read.
$this->NumResult = $this->NumResult - 1;
return @mysql_fetch_array($this->Query);
}
else return NULL;
}
else return NULL;
}
/*
* Returns number of affected rows by the query.
* Should use with an INSERT statement.
*/
public function AffectedRows() {
return $this->AffectedRows;
}
/*
* Returns total number of rows returned by the query.
* Should use with a SELECT statement.
*/
public function TotalRows() {
return $this->TotalRows;
}
}
----
Server IP: 190.54.16.132
Probable Submitter: 200.111.169.218
----
Manual Page -- http://www.php.net/manual/en/function.mysql-connect.php
Edit -- https://master.php.net/note/edit/98136
Del: integrated -- https://master.php.net/note/delete/98136/integrated
Del: useless -- https://master.php.net/note/delete/98136/useless
Del: bad code -- https://master.php.net/note/delete/98136/bad+code
Del: spam -- https://master.php.net/note/delete/98136/spam
Del: non-english -- https://master.php.net/note/delete/98136/non-english
Del: in docs -- https://master.php.net/note/delete/98136/in+docs
Del: other reasons-- https://master.php.net/note/delete/98136
Reject -- https://master.php.net/note/reject/98136
Search -- https://master.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.