RESSOURCES 
 
Accès à une base de données MySQL
Fourni par PHPHeaven.net 26 janvier 2001
 
 
/**************************************************
*
* MySql
* Nicolas Hoizey, 11/02/2001 00:02
* 
* Classe PHP d'accès à une base de données MySql
*
* @param dbHost Adresse IP (ou nom logique) du serveur mysql
* @param dbName Nom de la base de données mysql à activer
* @param dbUser Login de l'utilisateur pour la connexion
* @param dbPass Mot de passe de l'utilisateur pour la connexion
* @return TRUE si initialisation connexion ok
*
* @see 
*
*************************************************/

class MySql 
{
 var $dbHost = ""; // hostname of the MySQL server
 var $dbName = ""; // logical database name on that server
 var $dbUser = ""; // database authorized user
 var $dbPass = ""; // user's password
 var $linkId = 0; // last result of mysql_connect()
 var $queryId = 0; // last result of mysql_query()
 var $record = array(); // last record fetched
 var $currentRow; // current row number
 var $errorNumber = 0; // last error number
 var $errorMessage = ""; // last error message
 var $errorLocation = ""; // last error location

 // constructor
 function MySql($dbHost = "", $dbName = "", $dbUser = "", $dbPass = "")
 {
  $this->dbHost = $dbHost;
  $this->dbName = $dbName;
  $this->dbUser = $dbUser;
  $this->dbPass = $dbPass;
 }

 // error handler
 function updateError($location)
 {
  $this->errorNumber = mysql_errno();
  $this->errorMessage = mysql_error();
  $this->errorLocation = $location;
  if($this->errorNumber && SHOW_ERROR_MESSAGES)
  {
   echo('<br /><b>'.$this->errorLocation.'</b><br />'.$this->errorMessage);
   flush();
  }
 }

 function connect()
 {
  if($this->linkId == 0)
  {
   $this->linkId = mysql_connect( $this->dbHost, $this->dbUser, $this->dbPass );
   if(!$this->linkId)
   {
    $this->updateError('DB::connect()<br />mysql_connect');
    return false;
   }
  }
  return true;
 }

 function query($queryString)
 {
  if(!$this->connect())
  {
   return false;
  }
  if(!mysql_select_db($this->dbName, $this->linkId))
  {
   $this->updateError('DB::connect()<br />mysql_select_db');
   return false;
  }
  $this->queryId = mysql_query($queryString, $this->linkId);
  $this->updateError('DB::query('.$queryString.')<br />mysql_query');
  if(!$this->queryId)
  {
   return false;
  }
  $this->currentRow = 0;
  return true;
 }

 // returns all records in an array
 function queryAllRecords($queryString)
 {
  if(!$this->query($queryString))
  {
   return false;
  }
  $ret = array();
  while($line = $this->nextRecord())
  {
   $ret[] = $line;
  }
  return $ret;
 }

 // returns one record in an array
 function queryOneRecord($queryString)
 {
  if(!$this->query($queryString) || $this->numRows() != 1)
  {
   return false;
  }
  return $this->nextRecord();
 }

 // returns the next record in an array
 function nextRecord()
 {
  $this->record = mysql_fetch_array($this->queryId);
  $this->updateError('DB::nextRecord()<br />mysql_fetch_array');
  if(!$this->record || !is_array($this->record))
  {
   return false;
  }
  $this->currentRow++;
  return $this->record;
 }

 // returns number of rows returned by the last select query
 function numRows()
 {
  return mysql_num_rows($this->queryId);
 }
}
 
Accueil | Haut de page