note 98136 deleted from function.mysql-connect by danbrown

From: Date: Thu, 27 May 2010 20:10:18 +0000
Subject: note 98136 deleted from function.mysql-connect by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-169669@lists.php.net to get a copy of this message
Note Submitter: alejandroivan at ingenieros dot com ---- 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; } }

« previous php.notes (#169669) next »