| [ Index ] |
PHP Cross Reference of Nucleus CMS v3.51 code documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* 4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/) 5 * Copyright (C) 2002-2009 The Nucleus Group 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License 9 * as published by the Free Software Foundation; either version 2 10 * of the License, or (at your option) any later version. 11 * (see nucleus/documentation/index.html#license for more info) 12 */ 13 /** 14 * @license http://nucleuscms.org/license.txt GNU General Public License 15 * @copyright Copyright (C) 2002-2009 The Nucleus Group 16 * @version $Id$ 17 */ 18 19 /* 20 * complete sql_* wrappers for mysql functions 21 * 22 * functions moved from globalfunctions.php: sql_connect, sql_disconnect, sql_query 23 */ 24 25 26 $MYSQL_CONN = 0; 27 global $SQL_DBH; 28 $SQL_DBH = NULL; 29 30 if (!function_exists('sql_fetch_assoc')) 31 { 32 /** 33 * Errors before the database connection has been made 34 */ 35 function startUpError($msg, $title) { 36 ?> 37 <html xmlns="http://www.w3.org/1999/xhtml"> 38 <head><title><?php echo htmlspecialchars($title)?></title></head> 39 <body> 40 <h1><?php echo htmlspecialchars($title)?></h1> 41 <?php echo $msg?> 42 </body> 43 </html> 44 <?php exit; 45 } 46 47 /** 48 * Connects to mysql server 49 */ 50 function sql_connect_args($mysql_host = 'localhost', $mysql_user = '', $mysql_password = '', $mysql_database = '') { 51 global $MYSQL_HANDLER; 52 53 try { 54 if (strpos($mysql_host,':') === false) { 55 $host = $mysql_host; 56 $port = ''; 57 $portnum = ''; 58 } 59 else { 60 list($host,$port) = explode(":",$mysql_host); 61 if (isset($port)) { 62 $portnum = $port; 63 $port = ';port='.trim($port); 64 } 65 else { 66 $port = ''; 67 $portnum = ''; 68 } 69 } 70 71 switch ($MYSQL_HANDLER[1]) { 72 case 'sybase': 73 case 'dblib': 74 if (is_numeric($portnum)) $port = ':'.intval($portnum); 75 else $port = ''; 76 $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password); 77 break; 78 case 'mssql': 79 if (is_numeric($portnum)) $port = ','.intval($portnum); 80 else $port = ''; 81 $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password); 82 break; 83 case 'oci': 84 if (is_numeric($portnum)) $port = ':'.intval($portnum); 85 else $port = ''; 86 $DBH = new PDO($MYSQL_HANDLER[1].':dbname=//'.$host.$port.'/'.$mysql_database, $mysql_user, $mysql_password); 87 break; 88 case 'odbc': 89 if (is_numeric($portnum)) $port = ';PORT='.intval($portnum); 90 else $port = ''; 91 $DBH = new PDO($MYSQL_HANDLER[1].':DRIVER={IBM DB2 ODBC DRIVER};HOSTNAME='.$host.$port.';DATABASE='.$mysql_database.';PROTOCOL=TCPIP;UID='.$mysql_user.';PWD='.$mysql_password); 92 93 break; 94 case 'pgsql': 95 if (is_numeric($portnum)) $port = ';port='.intval($portnum); 96 else $port = ''; 97 $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password); 98 break; 99 case 'sqlite': 100 case 'sqlite2': 101 if (is_numeric($portnum)) $port = ':'.intval($portnum); 102 else $port = ''; 103 $DBH = new PDO($MYSQL_HANDLER[1].':'.$mysql_database, $mysql_user, $mysql_password); 104 break; 105 default: 106 //mysql 107 $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password); 108 break; 109 } 110 111 112 113 } catch (PDOException $e) { 114 $DBH =NULL; 115 startUpError('<p>a1 Error!: ' . $e->getMessage() . '</p>', 'Connect Error'); 116 } 117 //echo '<hr />DBH: '.print_r($DBH,true).'<hr />'; 118 return $DBH; 119 } 120 121 /** 122 * Connects to mysql server 123 */ 124 function sql_connect() { 125 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN, $MYSQL_HANDLER, $SQL_DBH; 126 $SQL_DBH = NULL; 127 try { 128 if (strpos($MYSQL_HOST,':') === false) { 129 $host = $MYSQL_HOST; 130 $port = ''; 131 } 132 else { 133 list($host,$port) = explode(":",$MYSQL_HOST); 134 if (isset($port)) { 135 $portnum = $port; 136 $port = ';port='.trim($port); 137 } 138 else { 139 $port = ''; 140 $portnum = ''; 141 } 142 } 143 144 switch ($MYSQL_HANDLER[1]) { 145 case 'sybase': 146 case 'dblib': 147 if (is_numeric($portnum)) $port = ':'.intval($portnum); 148 else $port = ''; 149 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD); 150 break; 151 case 'mssql': 152 if (is_numeric($portnum)) $port = ','.intval($portnum); 153 else $port = ''; 154 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD); 155 break; 156 case 'oci': 157 if (is_numeric($portnum)) $port = ':'.intval($portnum); 158 else $port = ''; 159 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':dbname=//'.$host.$port.'/'.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD); 160 break; 161 case 'odbc': 162 if (is_numeric($portnum)) $port = ';PORT='.intval($portnum); 163 else $port = ''; 164 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':DRIVER={IBM DB2 ODBC DRIVER};HOSTNAME='.$host.$port.';DATABASE='.$MYSQL_DATABASE.';PROTOCOL=TCPIP;UID='.$MYSQL_USER.';PWD='.$MYSQL_PASSWORD); 165 166 break; 167 case 'pgsql': 168 if (is_numeric($portnum)) $port = ';port='.intval($portnum); 169 else $port = ''; 170 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD); 171 break; 172 case 'sqlite': 173 case 'sqlite2': 174 if (is_numeric($portnum)) $port = ':'.intval($portnum); 175 else $port = ''; 176 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':'.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD); 177 break; 178 default: 179 //mysql 180 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD); 181 break; 182 } 183 184 //$SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD); 185 186 } catch (PDOException $e) { 187 $SQL_DBH = NULL; 188 startUpError('<p>a2 Error!: ' . $e->getMessage() . '</p>', 'Connect Error'); 189 } 190 // echo '<hr />DBH: '.print_r($SQL_DBH,true).'<hr />'; 191 $MYSQL_CONN &= $SQL_DBH; 192 return $SQL_DBH; 193 194 } 195 196 /** 197 * disconnects from SQL server 198 */ 199 function sql_disconnect(&$dbh=NULL) { 200 global $SQL_DBH; 201 if (is_null($dbh)) $SQL_DBH = NULL; 202 else $dbh = NULL; 203 } 204 205 function sql_close(&$dbh=NULL) { 206 global $SQL_DBH; 207 if (is_null($dbh)) $SQL_DBH = NULL; 208 else $dbh = NULL; 209 } 210 211 /** 212 * executes an SQL query 213 */ 214 function sql_query($query,$dbh=NULL) { 215 global $SQLCount,$SQL_DBH; 216 $SQLCount++; 217 //echo '<hr />SQL_DBH: '; 218 //print_r($SQL_DBH); 219 //echo '<hr />DBH: '; 220 //print_r($dbh); 221 //echo '<hr />'; 222 //echo $query.'<hr />'; 223 if (is_null($dbh)) $res = $SQL_DBH->query($query); 224 else $res = $dbh->query($query); 225 if ($res->errorCode() != '00000') { 226 $errors = $res->errorInfo(); 227 print("SQL error with query $query: " . $errors[0].'-'.$errors[1].' '.$errors[2] . '<p />'); 228 } 229 230 return $res; 231 } 232 233 /** 234 * executes an SQL error 235 */ 236 function sql_error($dbh=NULL) 237 { 238 global $SQL_DBH; 239 if (is_null($dbh)) $error = $SQL_DBH->errorInfo(); 240 else $error = $dbh->errorInfo(); 241 if ($error[0] != '00000') { 242 return $error[0].'-'.$error[1].' '.$error[2]; 243 } 244 else return ''; 245 } 246 247 /** 248 * executes an SQL db select 249 */ 250 function sql_select_db($db,&$dbh=NULL) 251 { 252 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN, $MYSQL_HANDLER, $SQL_DBH; 253 //echo '<hr />'.print_r($dbh,true).'<hr />'; 254 //exit; 255 if (is_null($dbh)) { 256 try { 257 $SQL_DBH = NULL; 258 list($host,$port) = explode(":",$MYSQL_HOST); 259 if (isset($port)) { 260 $portnum = $port; 261 $port = ';port='.trim($port); 262 } 263 else { 264 $port = ''; 265 $portnum = ''; 266 } 267 //$SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.trim($host).$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD); 268 //$SQL_DBH = sql_connect(); 269 switch ($MYSQL_HANDLER[1]) { 270 case 'sybase': 271 case 'dblib': 272 if (is_numeric($portnum)) $port = ':'.intval($portnum); 273 else $port = ''; 274 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD); 275 break; 276 case 'mssql': 277 if (is_numeric($portnum)) $port = ','.intval($portnum); 278 else $port = ''; 279 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD); 280 break; 281 case 'oci': 282 if (is_numeric($portnum)) $port = ':'.intval($portnum); 283 else $port = ''; 284 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':dbname=//'.$host.$port.'/'.$db, $MYSQL_USER, $MYSQL_PASSWORD); 285 break; 286 case 'odbc': 287 if (is_numeric($portnum)) $port = ';PORT='.intval($portnum); 288 else $port = ''; 289 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':DRIVER={IBM DB2 ODBC DRIVER};HOSTNAME='.$host.$port.';DATABASE='.$db.';PROTOCOL=TCPIP;UID='.$MYSQL_USER.';PWD='.$MYSQL_PASSWORD); 290 291 break; 292 case 'pgsql': 293 if (is_numeric($portnum)) $port = ';port='.intval($portnum); 294 else $port = ''; 295 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD); 296 break; 297 case 'sqlite': 298 case 'sqlite2': 299 if (is_numeric($portnum)) $port = ':'.intval($portnum); 300 else $port = ''; 301 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':'.$db, $MYSQL_USER, $MYSQL_PASSWORD); 302 break; 303 default: 304 //mysql 305 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD); 306 break; 307 } 308 return 1; 309 } catch (PDOException $e) { 310 startUpError('<p>a3 Error!: ' . $e->getMessage() . '</p>', 'Connect Error'); 311 return 0; 312 } 313 } 314 else { 315 if ($dbh->exec("USE $db") !== false) return 1; 316 else return 0; 317 } 318 } 319 320 /** 321 * executes an SQL real escape 322 */ 323 function sql_real_escape_string($val,$dbh=NULL) 324 { 325 return addslashes($val); 326 } 327 328 /** 329 * executes an PDO::quote() like escape, ie adds quotes arround the string and escapes chars as needed 330 */ 331 function sql_quote_string($val,$dbh=NULL) { 332 global $SQL_DBH; 333 if (is_null($dbh)) 334 return $SQL_DBH->quote($val); 335 else 336 return $dbh->quote($val); 337 } 338 339 /** 340 * executes an SQL insert id 341 */ 342 function sql_insert_id($dbh=NULL) 343 { 344 global $SQL_DBH; 345 if (is_null($dbh)) 346 return $SQL_DBH->lastInsertId(); 347 else 348 return $dbh->lastInsertId(); 349 } 350 351 /** 352 * executes an SQL result request 353 */ 354 function sql_result($res, $row = 0, $col = 0) 355 { 356 $results = array(); 357 if (intval($row) < 1) { 358 $results = $res->fetch(PDO::FETCH_BOTH); 359 return $results[$col]; 360 } 361 else { 362 for ($i = 0; $i < intval($row); $i++) { 363 $results = $res->fetch(PDO::FETCH_BOTH); 364 } 365 $results = $res->fetch(PDO::FETCH_BOTH); 366 return $results[$col]; 367 } 368 } 369 370 /** 371 * frees sql result resources 372 */ 373 function sql_free_result($res) 374 { 375 $res = NULL; 376 return true; 377 } 378 379 /** 380 * returns number of rows in SQL result 381 */ 382 function sql_num_rows($res) 383 { 384 return $res->rowCount(); 385 } 386 387 /** 388 * returns number of rows affected by SQL query 389 */ 390 function sql_affected_rows($res) 391 { 392 return $res->rowCount(); 393 } 394 395 /** 396 * Get number of fields in result 397 */ 398 function sql_num_fields($res) 399 { 400 return $res->columnCount(); 401 } 402 403 /** 404 * fetches next row of SQL result as an associative array 405 */ 406 function sql_fetch_assoc($res) 407 { 408 $results = array(); 409 $results = $res->fetch(PDO::FETCH_ASSOC); 410 return $results; 411 } 412 413 /** 414 * Fetch a result row as an associative array, a numeric array, or both 415 */ 416 function sql_fetch_array($res) 417 { 418 $results = array(); 419 $results = $res->fetch(PDO::FETCH_BOTH); 420 return $results; 421 } 422 423 /** 424 * fetches next row of SQL result as an object 425 */ 426 function sql_fetch_object($res) 427 { 428 $results = NULL; 429 $results = $res->fetchObject(); 430 return $results; 431 } 432 433 /** 434 * Get a result row as an enumerated array 435 */ 436 function sql_fetch_row($res) 437 { 438 $results = array(); 439 $results = $res->fetch(PDO::FETCH_NUM); 440 return $results; 441 } 442 443 /** 444 * Get column information from a result and return as an object 445 */ 446 function sql_fetch_field($res,$offset = 0) 447 { 448 $results = array(); 449 $obj = NULL; 450 $results = $res->getColumnMeta($offset); 451 foreach($results as $key=>$value) { 452 $obj->$key = $value; 453 } 454 return $obj; 455 } 456 457 /** 458 * Get current system status (returns string) 459 */ 460 function sql_stat($dbh=NULL) 461 { 462 //not implemented 463 global $SQL_DBH; 464 if (is_null($dbh)) 465 return ''; 466 else 467 return ''; 468 } 469 470 /** 471 * Returns the name of the character set 472 */ 473 function sql_client_encoding($dbh=NULL) 474 { 475 //not implemented 476 global $SQL_DBH; 477 if (is_null($dbh)) 478 return ''; 479 else 480 return ''; 481 } 482 483 /** 484 * Get SQL client version 485 */ 486 function sql_get_client_info() 487 { 488 global $SQL_DBH; 489 return $SQL_DBH->getAttribute(constant("PDO::ATTR_CLIENT_VERSION")); 490 } 491 492 /** 493 * Get SQL server version 494 */ 495 function sql_get_server_info($dbh=NULL) 496 { 497 global $SQL_DBH; 498 if (is_null($dbh)) 499 return $SQL_DBH->getAttribute(constant("PDO::ATTR_SERVER_VERSION")); 500 else 501 return $dbh->getAttribute(constant("PDO::ATTR_SERVER_VERSION")); 502 } 503 504 /** 505 * Returns a string describing the type of SQL connection in use for the connection or FALSE on failure 506 */ 507 function sql_get_host_info($dbh=NULL) 508 { 509 global $SQL_DBH; 510 if (is_null($dbh)) 511 return $SQL_DBH->getAttribute(constant("PDO::ATTR_SERVER_INFO")); 512 else 513 return $dbh->getAttribute(constant("PDO::ATTR_SERVER_INFO")); 514 } 515 516 /** 517 * Returns the SQL protocol on success, or FALSE on failure. 518 */ 519 function sql_get_proto_info($dbh=NULL) 520 { 521 //not implemented 522 global $SQL_DBH; 523 if (is_null($dbh)) 524 return false; 525 else 526 return false; 527 } 528 529 /** 530 * Get the name of the specified field in a result 531 */ 532 function sql_field_name($res, $offset = 0) 533 { 534 $column = $res->getColumnMeta($offset); 535 if ($column) { 536 return $column['name']; 537 } 538 return false; 539 } 540 541 /************************************************************************** 542 Unimplemented mysql_* functions 543 544 # mysql_ data_ seek (maybe useful) 545 # mysql_ errno (maybe useful) 546 # mysql_ fetch_ lengths (maybe useful) 547 # mysql_ field_ flags (maybe useful) 548 # mysql_ field_ len (maybe useful) 549 # mysql_ field_ seek (maybe useful) 550 # mysql_ field_ table (maybe useful) 551 # mysql_ field_ type (maybe useful) 552 # mysql_ info (maybe useful) 553 # mysql_ list_ processes (maybe useful) 554 # mysql_ ping (maybe useful) 555 # mysql_ set_ charset (maybe useful, requires php >=5.2.3 and mysql >=5.0.7) 556 # mysql_ thread_ id (maybe useful) 557 558 # mysql_ db_ name (useful only if working on multiple dbs which we do not do) 559 # mysql_ list_ dbs (useful only if working on multiple dbs which we do not do) 560 561 # mysql_ pconnect (probably not useful and could cause some unintended performance issues) 562 # mysql_ unbuffered_ query (possibly useful, but complicated and not supported by all database drivers (pdo)) 563 564 # mysql_ change_ user (deprecated) 565 # mysql_ create_ db (deprecated) 566 # mysql_ db_ query (deprecated) 567 # mysql_ drop_ db (deprecated) 568 # mysql_ escape_ string (deprecated) 569 # mysql_ list_ fields (deprecated) 570 # mysql_ list_ tables (deprecated) 571 # mysql_ tablename (deprecated) 572 573 *******************************************************************/ 574 575 } 576 577 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sun Aug 1 03:56:06 2010 |