Bonjour,
J'ai créé une fonction MySQLi (qui peut peut être servir pour d'autres) que je souhaite optimiser. Car j'ai l'impression que dans ma fonction query, j’exécute 2 fois la requête :
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 | <?php class sql { public $db; public $config = Array(); public $affected_rows; public $num_rows; public $last_id; public $sql; public function __construct() { include('config.php'); $this->db = mysqli_connect($config['HOST'],$config['USER'],$config['PASSWORD'],$config['DBNAME']); if ($this->db->connect_errno) { die("Echec lors de la connexion à MySQL"); } else { $this->db->query("SET NAMES UTF8"); $rq = $this->db->query("SELECT `name`,`value` FROM `config` ORDER BY `id` ASC"); while($rp = $this->fetch($rq)) { $this->config[$rp['name']] = $rp['value']; } return true; } } public function get_user_ip() { if($_SERVER) { if(@$_SERVER['HTTP_X_FORWARDED_FOR']) $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; elseif(@$_SERVER['HTTP_CLIENT_IP']) $ip = $_SERVER['HTTP_CLIENT_IP']; else $ip = $_SERVER['REMOTE_ADDR']; } else { if(getenv('HTTP_X_FORWARDED_FOR')) $ip = getenv('HTTP_X_FORWARDED_FOR'); elseif(getenv('HTTP_CLIENT_IP')) $ip = getenv('HTTP_CLIENT_IP'); else $ip = getenv('REMOTE_ADDR'); } return $ip; } public function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } public function result($rq) { $res = mysqli_fetch_row($rq); if($res) { return $res[0]; } else { return false; } } public function results($rq) { $res = mysqli_fetch_row($rq); if($res) { return $res; } else { return false; } } public function num_results($sql) { $nums = Array(); $rq = $this->db->query($sql); while($row = $rq->fetch_array(MYSQLI_NUM)) { $nums[] = $row[0]; } if(count($nums)) { return $nums; } else { return false; } } public function query($sql) { if($sql) { $this->sql = $sql; if(preg_match('#INSERT#i',$sql)) { $rq = $this->db->query($sql); $this->last_id = $this->db->insert_id; return $rq; } else if(preg_match('#(UPDATE|DELETE|SELECT|KILL|SHOW)#i',$sql)) { $stmt = $this->db->stmt_init(); if(!$stmt->prepare($sql)) { die("SQL error in query {$sql}"); } $stmt->execute(); if(preg_match('#SELECT#i',$sql)) $stmt->store_result(); $this->num_rows = trim(str_replace('-','',(int)$stmt->affected_rows)); $this->affected_rows = $this->num_rows; $stmt->close(); $rq = $this->db->query($sql); return $rq; } else { return false; } } else { die("SQL error in query {$sql}"); } } public function close() { $this->db->close(); } public function fetch($query) { if($query) { $rows = $query->fetch_array(MYSQLI_ASSOC); $this->affected_rows = count($rows); return $rows; } else return false; } } ?> |
Merci pour votre aide.
Sylvain
+0
-0