1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| class database_1 {
// var $dbh;
// var $db;
// var $cursor;
// var $nbrows;
// var $row;
function __construct($dbh, $db, $cursor,$nbrows,$row ) {
$this->dbh = $dbh;
$this->db = $db;
$this->cursor = $cursor;
$this->nbrows = $nbrows;
$this->row = $row;
}
function database($host,$user,$pass,$name) {
$this->dbh = mysqli_connect($host,$user,$pass);
//$this->dbh = mysql_pconnect($host,$user,$pass);
if (!$this->dbh) { die($host."___".$user."_____Server connexion failed");}
$this->dbh->query("SET NAMES 'latin1'");
if (!$this->select_db($name)) {
die($name." DB connexion failed ");
return false;
}
} // constructeur
function select_db($db) {
// Selectionne la database à utiliser. Si le paramètre de la fonction select est
// passé à la fonction database la variable de class sera mise à jour.
if (!empty($db)) $this->db = $db;
if (!$this->dbh->select_db($this->db)) {
// if (!mysql_select_db($this->db)) {
$this->last_error = mysql_error();
return false;
}
return true;
}
function Query($sql,$action) {
$this->nbrows = -1;
if (!$this->cursor = mysqli_query($this->dbh,$sql))
{
if ($action == 'no_error') {echo mysql_error();'<br>'; return false;}
else {die("[".$sql."] : ".mysql_error());}
}
$query_type = array("insert","delete","update");
// loop through the above array
foreach ( $query_type as $word ) {
// This is true if the query starts with insert, delete or update
if ( preg_match("/$word /i",$sql) ) { $this->nbrows = mysqli_affected_rows($this->dbh); }
}
return $this->nbrows;
} // Query
function Select($sql) {
// if (!$this->cursor = mysql_query($sql,$this->dbh)) { die("[".$sql."] : ".mysql_error()); }
if (!$this->cursor = mysqli_query($this->dbh,$sql)) { die("[".$sql."] : ".mysql_error()); }
$this->nbrows = $this->cursor->num_rows;
return $this->cursor;
} // Select
function SelectOne($sql) {
$this->Select($sql);
if ($this->nbrows == 1) { $this->row = mysqli_fetch_object($this->cursor); }
else { $this->row = false; }
return $this->row;
}
// SelectOne
function fetchObject() {
if (!$this->cursor) return false;
$this->row = $this->cursor->fetch_object();
return $this->row;
}
function insertid() {
if (!empty($db)) $this->db = $db;
if (!$this->dbh->select_db($this->db)) {
$this->last_error = mysql_error();
return false;
}
return (mysqli_insert_id($this->dbh));
}
} // class |
Partager