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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
<?php
class MySQL
{
private $host;
private $db;
private $user, $pwd;
private $ConnectLink, $rConnectLink;
private $rResult;
private $TypeFetch;
private $NbrOfResult, $iLastId, $sQuery;
##################################CONSTRUCTEUR############################################
public function MySQL($host = '', $db = '', $user = '', $pwd = '')
{
$this->host = $host;
$this->db = $db;
$this->user = $user;
$this->pwd = $pwd;
$this->ConnectLink = array();
$this->rResult = 0;
$this->TypeFetch = 'assoc';
$this->NbrOfResult = 0;
$this->iLastId = 0;
$this->sQuery = 0;
$this->connexionDB($this->host, $this->user, $this->pwd, $this->db);
}
/**
* Connexion Serveur MYSQL
*
* @author Dearth
* @version 2.0
* @param string $ h,u,p,d as Serveur MYSQL
* @access public
*/
public function connexionDB($h, $u, $p, $d)
{
if( $h == null || $u == null || $p == null )
{
throw new Error('Erreur : Host ou Utilisateur ou Password ne contient rien.');
exit();
}
else
{
try
{
$connectId = mysql_pconnect($h, $u, $p);
array_push($this->ConnectLink, $connectId);
$this->selectDB($d);
}
catch(Error $e)
{
echo $e.'<br>';
exit();
}
}
}
/**
* Select une db
*
* @author Dearth
* @version 2.0
* @param string $db as database
* @access public
*/
public function selectDB($db)
{
if($db == null)
{
throw new Error('Erreur : Aucune db selectionn�e.');
exit();
}
else
{
try
{
$link = array_pop($this->ConnectLink);
$this->rConnectLink = $link;
mysql_select_db($db, $link);
}
catch(Error $e)
{
echo $e.'<br>';
exit();
}
}
}
/**
* Execute une requete
*
* @author Dearth
* @version 2.0
* @param string $execute
* @access public
*/
public function query($execute, $connectId = null)
{
if($connectId == null)
$connectId = $this->rConnectLink;
$this->sQuery = trim($execute);
echo $this->sQuery."<hr>";
$this->rResult = mysql_query($this->sQuery, $connectId);
if(false === $this->rResult)
{
exit( mysql_error() );
return false;
}
$this->checkQuery();
return $this->rResult;
}
/**
* Protege une variable
*
* @author Dearth
* @version 2.0
* @param string $String Type de parsage.
* @access public
*/
public function protect($String)
{
$String = htmlentities(trim($String, " \0"), ENT_NOQUOTES);
$String = mysql_real_escape_string($String, $this->rConnectLink);
return $String;
}
/**
* Retourne le type de champs
*
* @author Dearth
* @version 2.0
* @param resultId as Resultat de la dernière execution query
* @access public
*/
public function Type($resultId = null)
{
if($resultId == null)
$resultId = $this->rResult;
switch($this->TypeFetch)
{
case 'assoc':
return mysql_fetch_assoc($resultId);
break;
case 'array':
return mysql_fetch_array($resultId);
break;
case 'row':
return mysql_fetch_row($resultId);
break;
case 'object':
return mysql_fetch_object($resultId);
break;
case 'field':
return mysql_fetch_object($resultId);
break;
default:
return mysql_fetch_assoc($resultId);
break;
}
}
/**
* Verifie le type de requete puis l'adapte
*
* @author Dearth
* @version 2.0
* @param string $type;
* @access public
*/
public function checkQuery()
{
$sTypeQuery = strtolower(substr(trim($this->sQuery), 0,6));
switch($sTypeQuery)
{
case 'insert': $this->iLastId = @mysql_insert_id($this->rConnectLink);
case 'update':
case 'delete': $this->NbOfResult = @mysql_affected_rows($this->rConnectLink); break;
case 'select': $this->NbOfResult = @mysql_num_rows($this->rResult); break;
default: throw new Error("La requete ne commence pas par SELECT ni INSERT, UPDATE ou DELETE"); break;
}
}
/**
* Initialise le type de parsage des résultats.
*
* @author Dearth
* @version 2.0
* @param string $fetch Type de parsage.
* @access public
*/
public function setFetchType($fetch)
{
$this->TypeFetch = $fetch;
}
#Retourne le nbr de ligne
public function getNbrOfResult()
{
return $this->NbOfResult;
}
#Retourne le dernier id inseré
public function getLastId()
{
return $this->iLastId;
}
}
?> |
Partager