2009
05.22

Hello again,

I thought i’ll share another one of my little personal scripts with you guys.

Here is the mysql class I use on most of my project, nothing fancy but does the work for what I need most of the time.

Since it use Singleton you have to call it like that

$m = Mysql::singleton();
<?php

class Mysql{

 private $serveur = "localhost";
 private $login   = "admin";
 private $pass    = "***";
 private $bdd     = "bdd_site";
 private static $instance;

 private function __construct(){
 $this->connect();
 }

 public function connect(){
 @mysql_connect($this->serveur,$this->login,$this->pass) or die("<b>Error : </b><br /><pre>".mysql_error()."</pre>");
 @mysql_select_db($this->bdd) or die("<b>Error : </b><br /><pre>".mysql_error()."</pre>");
 $this->q("set names 'utf8'");
 }

 public function q($query){
 $r = @mysql_query($query) or die("<b>Error : </b><br />$query<br /><pre>".mysql_error()."</pre>");
 return $r;
 }

 public function qr($query){
 $r = $this->q($query);
 if(mysql_num_rows($r)>0){
 if(mysql_num_rows($r)==1){
 return mysql_fetch_array($r);
 } else {
 $res = array();
 while($arr = mysql_fetch_array($r)){
 array_push($res,$arr);
 }
 return $res;
 }
 } else {
 return NULL;
 }
 }

 //num rows tt simple
 public function nr($query){
 return mysql_num_rows($this->q($query));
 }

 //last id
 public function lid(){
 return mysql_insert_id();
 }

 // fermeture de la connect mysql
 public function c(){
 mysql_close();
 }

 /* singleton */

 public static function singleton(){
 if (!isset(self::$instance)) {
 $c = __CLASS__;
 self::$instance = new $c;
 }
 return self::$instance;
 }

 public function __clone()
 {
 trigger_error('no clone', E_USER_ERROR);
 }

}

?>
  • Share/Bookmark

Related Posts:

No Comment.

Add Your Comment