| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /** |
|---|
| 4 | * Session Management for PHP3 |
|---|
| 5 | * |
|---|
| 6 | * Copyright (c) 1998-2000 NetUSE AG |
|---|
| 7 | * Boris Erdmann, Kristian Koehntopp |
|---|
| 8 | * |
|---|
| 9 | * $Id: db_mysql.php,v 1.3 2005/03/05 16:27:30 said Exp $ |
|---|
| 10 | * |
|---|
| 11 | */ |
|---|
| 12 | |
|---|
| 13 | class DB_Sql { |
|---|
| 14 | |
|---|
| 15 | /* public: connection parameters */ |
|---|
| 16 | var $Host = ""; |
|---|
| 17 | var $Database = ""; |
|---|
| 18 | var $User = ""; |
|---|
| 19 | var $Password = ""; |
|---|
| 20 | |
|---|
| 21 | /* public: configuration parameters */ |
|---|
| 22 | var $Auto_Free = 0; ## Set to 1 for automatic mysql_free_result() |
|---|
| 23 | var $Debug = 0; ## Set to 1 for debugging messages. |
|---|
| 24 | var $Halt_On_Error = "no"; ## "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning) |
|---|
| 25 | var $Seq_Table = "db_sequence"; |
|---|
| 26 | |
|---|
| 27 | /* public: result array and current row number */ |
|---|
| 28 | var $Record = array(); |
|---|
| 29 | var $Row; |
|---|
| 30 | |
|---|
| 31 | /* public: current error number and error text */ |
|---|
| 32 | var $Errno = 0; |
|---|
| 33 | var $Error = ""; |
|---|
| 34 | |
|---|
| 35 | /* public: this is an api revision, not a CVS revision. */ |
|---|
| 36 | var $type = "mysql"; |
|---|
| 37 | var $revision = "1.2"; |
|---|
| 38 | |
|---|
| 39 | /* private: link and query handles */ |
|---|
| 40 | var $Link_ID = 0; |
|---|
| 41 | var $Query_ID = 0; |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | * Constructor |
|---|
| 47 | */ |
|---|
| 48 | function DB_Sql($query = "") { |
|---|
| 49 | $this->query($query); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | * @return the class variable Link_ID |
|---|
| 54 | */ |
|---|
| 55 | function link_id() { |
|---|
| 56 | return $this->Link_ID; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | * @return the class variable Query_ID |
|---|
| 61 | */ |
|---|
| 62 | function query_id() { |
|---|
| 63 | return $this->Query_ID; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | /** |
|---|
| 67 | * function for MySQL database connection management |
|---|
| 68 | * |
|---|
| 69 | * This function manages the connection to the MySQL database. |
|---|
| 70 | * |
|---|
| 71 | * @param $Database name of the database |
|---|
| 72 | * @param $Host DNS of the MySQL hosting server |
|---|
| 73 | * @param $User the user's name |
|---|
| 74 | * @param $Password the user's password |
|---|
| 75 | * |
|---|
| 76 | * @return the class variable $Link_ID |
|---|
| 77 | */ |
|---|
| 78 | function connect($Database = "", $Host = "", $User = "", $Password = "") { |
|---|
| 79 | /* Handle defaults */ |
|---|
| 80 | if ("" == $Database) |
|---|
| 81 | $Database = $this->Database; |
|---|
| 82 | if ("" == $Host) |
|---|
| 83 | $Host = $this->Host; |
|---|
| 84 | if ("" == $User) |
|---|
| 85 | $User = $this->User; |
|---|
| 86 | if ("" == $Password) |
|---|
| 87 | $Password = $this->Password; |
|---|
| 88 | |
|---|
| 89 | /* establish connection, select database */ |
|---|
| 90 | if ( 0 == $this->Link_ID ) { |
|---|
| 91 | |
|---|
| 92 | $this->Link_ID=mysql_pconnect($Host, $User, $Password); |
|---|
| 93 | if (!$this->Link_ID) { |
|---|
| 94 | $this->halt("pconnect($Host, $User, \$Password) failed."); |
|---|
| 95 | return 0; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | if (!@mysql_select_db($Database,$this->Link_ID)) { |
|---|
| 99 | $this->halt("cannot use database ".$this->Database); |
|---|
| 100 | return 0; |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | //persistent connection don't conserve database selection |
|---|
| 105 | //if needed do a correct database selection |
|---|
| 106 | $db_connected = @mysql_fetch_array(@mysql_query("SELECT DATABASE();",$this->Link_ID)); |
|---|
| 107 | if ($db_connected[0] != $this->Database) |
|---|
| 108 | mysql_select_db($Database,$this->Link_ID); |
|---|
| 109 | |
|---|
| 110 | return $this->Link_ID; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | /** |
|---|
| 114 | * Discard the query result |
|---|
| 115 | * |
|---|
| 116 | * This function discards the last query result. |
|---|
| 117 | */ |
|---|
| 118 | function free() { |
|---|
| 119 | @mysql_free_result($this->Query_ID); |
|---|
| 120 | $this->Query_ID = 0; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | /** |
|---|
| 124 | * Perform a query |
|---|
| 125 | * |
|---|
| 126 | * This function performs the MySQL query described in the string parameter |
|---|
| 127 | * |
|---|
| 128 | * @param a string describing the MySQL query |
|---|
| 129 | * @return the $Query_ID class variable (null if fails) |
|---|
| 130 | */ |
|---|
| 131 | function query($Query_String) { |
|---|
| 132 | /* No empty queries, please, since PHP4 chokes on them. */ |
|---|
| 133 | if ($Query_String == "") |
|---|
| 134 | /* The empty query string is passed on from the constructor, |
|---|
| 135 | * when calling the class without a query, e.g. in situations |
|---|
| 136 | * like these: '$db = new DB_Sql_Subclass;' |
|---|
| 137 | */ |
|---|
| 138 | return 0; |
|---|
| 139 | |
|---|
| 140 | if (!$this->connect()) { |
|---|
| 141 | return 0; /* we already complained in connect() about that. */ |
|---|
| 142 | }; |
|---|
| 143 | |
|---|
| 144 | # New query, discard previous result. |
|---|
| 145 | if ($this->Query_ID) { |
|---|
| 146 | $this->free(); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | if ($this->Debug) |
|---|
| 150 | printf("Debug: query = %s<br />\n", $Query_String); |
|---|
| 151 | |
|---|
| 152 | $this->Query_ID = @mysql_query($Query_String,$this->Link_ID); |
|---|
| 153 | $this->Row = 0; |
|---|
| 154 | $this->Errno = mysql_errno(); |
|---|
| 155 | $this->Error = mysql_error(); |
|---|
| 156 | if (!$this->Query_ID) { |
|---|
| 157 | $this->halt("Invalid SQL: ".$Query_String); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | # Will return nada if it fails. That's fine. |
|---|
| 161 | return $this->Query_ID; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | /** |
|---|
| 165 | * walk result set |
|---|
| 166 | * |
|---|
| 167 | * This function tests if a new record is available in the current |
|---|
| 168 | * query result. |
|---|
| 169 | * |
|---|
| 170 | * @return TRUE if a new record is available |
|---|
| 171 | */ |
|---|
| 172 | function next_record() { |
|---|
| 173 | if (!$this->Query_ID) { |
|---|
| 174 | $this->halt("next_record called with no query pending."); |
|---|
| 175 | return 0; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | $this->Record = @mysql_fetch_array($this->Query_ID); |
|---|
| 179 | $this->Row += 1; |
|---|
| 180 | $this->Errno = mysql_errno(); |
|---|
| 181 | $this->Error = mysql_error(); |
|---|
| 182 | |
|---|
| 183 | $stat = is_array($this->Record); |
|---|
| 184 | if (!$stat && $this->Auto_Free) { |
|---|
| 185 | $this->free(); |
|---|
| 186 | } |
|---|
| 187 | return $stat; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | /** |
|---|
| 191 | * |
|---|
| 192 | * public: position in result set |
|---|
| 193 | */ |
|---|
| 194 | |
|---|
| 195 | function seek($pos = 0) { |
|---|
| 196 | $status = @mysql_data_seek($this->Query_ID, $pos); |
|---|
| 197 | if ($status) |
|---|
| 198 | $this->Row = $pos; |
|---|
| 199 | else { |
|---|
| 200 | $this->halt("seek($pos) failed: result has ".$this->num_rows()." rows"); |
|---|
| 201 | |
|---|
| 202 | /* half assed attempt to save the day, |
|---|
| 203 | * but do not consider this documented or even |
|---|
| 204 | * desireable behaviour. |
|---|
| 205 | */ |
|---|
| 206 | @mysql_data_seek($this->Query_ID, $this->num_rows()); |
|---|
| 207 | $this->Row = $this->num_rows; |
|---|
| 208 | return 0; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | return 1; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | /* public: table locking */ |
|---|
| 215 | function lock($table, $mode="write") { |
|---|
| 216 | $this->connect(); |
|---|
| 217 | |
|---|
| 218 | $query="lock tables "; |
|---|
| 219 | if (is_array($table)) { |
|---|
| 220 | while (list($key,$value)=each($table)) { |
|---|
| 221 | if ($key=="read" && $key!=0) { |
|---|
| 222 | $query.="$value read, "; |
|---|
| 223 | } else { |
|---|
| 224 | $query.="$value $mode, "; |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | $query=substr($query,0,-2); |
|---|
| 228 | } else { |
|---|
| 229 | $query.="$table $mode"; |
|---|
| 230 | } |
|---|
| 231 | $res = @mysql_query($query, $this->Link_ID); |
|---|
| 232 | if (!$res) { |
|---|
| 233 | $this->halt("lock($table, $mode) failed."); |
|---|
| 234 | return 0; |
|---|
| 235 | } |
|---|
| 236 | return $res; |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | function unlock() { |
|---|
| 240 | $this->connect(); |
|---|
| 241 | |
|---|
| 242 | $res = @mysql_query("unlock tables", $this->Link_ID); |
|---|
| 243 | if (!$res) { |
|---|
| 244 | $this->halt("unlock() failed."); |
|---|
| 245 | return 0; |
|---|
| 246 | } |
|---|
| 247 | return $res; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | /* public: evaluate the result (size, width) */ |
|---|
| 252 | function affected_rows() { |
|---|
| 253 | return @mysql_affected_rows($this->Link_ID); |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | function num_rows() { |
|---|
| 257 | return @mysql_num_rows($this->Query_ID); |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | function num_fields() { |
|---|
| 261 | return @mysql_num_fields($this->Query_ID); |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | /* public: shorthand notation */ |
|---|
| 265 | function nf() { |
|---|
| 266 | return $this->num_rows(); |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | function np() { |
|---|
| 270 | print $this->num_rows(); |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | function f($Name) { |
|---|
| 274 | return $this->Record[$Name]; |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | function p($Name) { |
|---|
| 278 | print $this->Record[$Name]; |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | function lastid() { |
|---|
| 282 | return @mysql_insert_id($this->Link_ID); |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | /* public: sequence numbers */ |
|---|
| 286 | function nextid($seq_name) { |
|---|
| 287 | $this->connect(); |
|---|
| 288 | |
|---|
| 289 | if ($this->lock($this->Seq_Table)) { |
|---|
| 290 | /* get sequence number (locked) and increment */ |
|---|
| 291 | $q = sprintf("select nextid from %s where seq_name = '%s'", |
|---|
| 292 | $this->Seq_Table, |
|---|
| 293 | $seq_name); |
|---|
| 294 | $id = @mysql_query($q, $this->Link_ID); |
|---|
| 295 | $res = @mysql_fetch_array($id); |
|---|
| 296 | |
|---|
| 297 | /* No current value, make one */ |
|---|
| 298 | if (!is_array($res)) { |
|---|
| 299 | $currentid = 0; |
|---|
| 300 | $q = sprintf("insert into %s values('%s', %s)", |
|---|
| 301 | $this->Seq_Table, |
|---|
| 302 | $seq_name, |
|---|
| 303 | $currentid); |
|---|
| 304 | $id = @mysql_query($q, $this->Link_ID); |
|---|
| 305 | } else { |
|---|
| 306 | $currentid = $res["nextid"]; |
|---|
| 307 | } |
|---|
| 308 | $nextid = $currentid + 1; |
|---|
| 309 | $q = sprintf("update %s set nextid = '%s' where seq_name = '%s'", |
|---|
| 310 | $this->Seq_Table, |
|---|
| 311 | $nextid, |
|---|
| 312 | $seq_name); |
|---|
| 313 | $id = @mysql_query($q, $this->Link_ID); |
|---|
| 314 | $this->unlock(); |
|---|
| 315 | } else { |
|---|
| 316 | $this->halt("cannot lock ".$this->Seq_Table." - has it been created?"); |
|---|
| 317 | return 0; |
|---|
| 318 | } |
|---|
| 319 | return $nextid; |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | /* public: return table metadata */ |
|---|
| 323 | function metadata($table='',$full=false) { |
|---|
| 324 | $count = 0; |
|---|
| 325 | $id = 0; |
|---|
| 326 | $res = array(); |
|---|
| 327 | |
|---|
| 328 | /* |
|---|
| 329 | * Due to compatibility problems with Table we changed the behavior |
|---|
| 330 | * of metadata(); |
|---|
| 331 | * depending on $full, metadata returns the following values: |
|---|
| 332 | * |
|---|
| 333 | * - full is false (default): |
|---|
| 334 | * $result[]: |
|---|
| 335 | * [0]["table"] table name |
|---|
| 336 | * [0]["name"] field name |
|---|
| 337 | * [0]["type"] field type |
|---|
| 338 | * [0]["len"] field length |
|---|
| 339 | * [0]["flags"] field flags |
|---|
| 340 | * |
|---|
| 341 | * - full is true |
|---|
| 342 | * $result[]: |
|---|
| 343 | * ["num_fields"] number of metadata records |
|---|
| 344 | * [0]["table"] table name |
|---|
| 345 | * [0]["name"] field name |
|---|
| 346 | * [0]["type"] field type |
|---|
| 347 | * [0]["len"] field length |
|---|
| 348 | * [0]["flags"] field flags |
|---|
| 349 | * ["meta"][field name] index of field named "field name" |
|---|
| 350 | * The last one is used, if you have a field name, but no index. |
|---|
| 351 | * Test: if (isset($result['meta']['myfield'])) { ... |
|---|
| 352 | */ |
|---|
| 353 | |
|---|
| 354 | // if no $table specified, assume that we are working with a query |
|---|
| 355 | // result |
|---|
| 356 | if ($table) { |
|---|
| 357 | $this->connect(); |
|---|
| 358 | $id = @mysql_list_fields($this->Database, $table); |
|---|
| 359 | if (!$id) |
|---|
| 360 | $this->halt("Metadata query failed."); |
|---|
| 361 | } else { |
|---|
| 362 | $id = $this->Query_ID; |
|---|
| 363 | if (!$id) |
|---|
| 364 | $this->halt("No query specified."); |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | $count = @mysql_num_fields($id); |
|---|
| 368 | |
|---|
| 369 | // made this IF due to performance (one if is faster than $count if's) |
|---|
| 370 | if (!$full) { |
|---|
| 371 | for ($i=0; $i<$count; $i++) { |
|---|
| 372 | $res[$i]["table"] = @mysql_field_table ($id, $i); |
|---|
| 373 | $res[$i]["name"] = @mysql_field_name ($id, $i); |
|---|
| 374 | $res[$i]["type"] = @mysql_field_type ($id, $i); |
|---|
| 375 | $res[$i]["len"] = @mysql_field_len ($id, $i); |
|---|
| 376 | $res[$i]["flags"] = @mysql_field_flags ($id, $i); |
|---|
| 377 | } |
|---|
| 378 | } else { // full |
|---|
| 379 | $res["num_fields"]= $count; |
|---|
| 380 | |
|---|
| 381 | for ($i=0; $i<$count; $i++) { |
|---|
| 382 | $res[$i]["table"] = @mysql_field_table ($id, $i); |
|---|
| 383 | $res[$i]["name"] = @mysql_field_name ($id, $i); |
|---|
| 384 | $res[$i]["type"] = @mysql_field_type ($id, $i); |
|---|
| 385 | $res[$i]["len"] = @mysql_field_len ($id, $i); |
|---|
| 386 | $res[$i]["flags"] = @mysql_field_flags ($id, $i); |
|---|
| 387 | $res["meta"][$res[$i]["name"]] = $i; |
|---|
| 388 | } |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | // free the result only if we were called on a table |
|---|
| 392 | if ($table) @mysql_free_result($id); |
|---|
| 393 | return $res; |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | /********************************************************************************************************/ |
|---|
| 397 | // AJOUT PERSO : TEST |
|---|
| 398 | |
|---|
| 399 | /* public: return table metadata |
|---|
| 400 | function retourneNameField($this->Query_ID,$full=false) { |
|---|
| 401 | $count = 0; |
|---|
| 402 | $id = 0; |
|---|
| 403 | $res = array(); |
|---|
| 404 | |
|---|
| 405 | |
|---|
| 406 | $count = @mysql_num_fields($this->Query_ID); |
|---|
| 407 | |
|---|
| 408 | // made this IF due to performance (one if is faster than $count if's) |
|---|
| 409 | if (!$full) { |
|---|
| 410 | for ($i=0; $i<$count; $i++) { |
|---|
| 411 | $res[$i]["table"] = @mysql_field_table ($id, $i); |
|---|
| 412 | $res[$i]["name"] = @mysql_field_name ($id, $i); |
|---|
| 413 | $res[$i]["type"] = @mysql_field_type ($id, $i); |
|---|
| 414 | $res[$i]["len"] = @mysql_field_len ($id, $i); |
|---|
| 415 | $res[$i]["flags"] = @mysql_field_flags ($id, $i); |
|---|
| 416 | } |
|---|
| 417 | } else { // full |
|---|
| 418 | $res["num_fields"]= $count; |
|---|
| 419 | |
|---|
| 420 | for ($i=0; $i<$count; $i++) { |
|---|
| 421 | $res[$i]["table"] = @mysql_field_table ($id, $i); |
|---|
| 422 | $res[$i]["name"] = @mysql_field_name ($id, $i); |
|---|
| 423 | $res[$i]["type"] = @mysql_field_type ($id, $i); |
|---|
| 424 | $res[$i]["len"] = @mysql_field_len ($id, $i); |
|---|
| 425 | $res[$i]["flags"] = @mysql_field_flags ($id, $i); |
|---|
| 426 | $res["meta"][$res[$i]["name"]] = $i; |
|---|
| 427 | } |
|---|
| 428 | } |
|---|
| 429 | |
|---|
| 430 | // free the result only if we were called on a table |
|---|
| 431 | if ($table) @mysql_free_result($id); |
|---|
| 432 | return $res; |
|---|
| 433 | }*/ |
|---|
| 434 | |
|---|
| 435 | /********************************************************************************************************/ |
|---|
| 436 | /* private: error handling */ |
|---|
| 437 | function halt($msg) { |
|---|
| 438 | $this->Error = @mysql_error($this->Link_ID); |
|---|
| 439 | $this->Errno = @mysql_errno($this->Link_ID); |
|---|
| 440 | if ($this->Halt_On_Error == "no") |
|---|
| 441 | return; |
|---|
| 442 | |
|---|
| 443 | $this->haltmsg($msg); |
|---|
| 444 | |
|---|
| 445 | if ($this->Halt_On_Error != "report") |
|---|
| 446 | die("Session halted."); |
|---|
| 447 | } |
|---|
| 448 | |
|---|
| 449 | function haltmsg($msg) { |
|---|
| 450 | printf("</td></tr></table><b>Database error:</b> %s<br />\n", $msg); |
|---|
| 451 | printf("<b>MySQL Error</b>: %s (%s)<br />\n", |
|---|
| 452 | $this->Errno, |
|---|
| 453 | $this->Error); |
|---|
| 454 | } |
|---|
| 455 | |
|---|
| 456 | function table_names() { |
|---|
| 457 | $this->query("SHOW TABLES"); |
|---|
| 458 | $i=0; |
|---|
| 459 | while ($info=mysql_fetch_row($this->Query_ID)) |
|---|
| 460 | { |
|---|
| 461 | $return[$i]["table_name"]= $info[0]; |
|---|
| 462 | $return[$i]["tablespace_name"]=$this->Database; |
|---|
| 463 | $return[$i]["database"]=$this->Database; |
|---|
| 464 | $i++; |
|---|
| 465 | } |
|---|
| 466 | return $return; |
|---|
| 467 | } |
|---|
| 468 | } |
|---|
| 469 | ?> |
|---|