4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
5 * Copyright (C) 2002-2012 The Nucleus Group
\r
7 * This program is free software; you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation; either version 2
\r
10 * of the License, or (at your option) any later version.
\r
11 * (see nucleus/documentation/index.html#license for more info)
\r
14 * @license http://nucleuscms.org/license.txt GNU General Public License
\r
15 * @copyright Copyright (C) 2002-2012 The Nucleus Group
\r
20 * complete sql_* wrappers for mysql functions
\r
22 * functions moved from globalfunctions.php: sql_connect, sql_disconnect, sql_query
\r
30 if (!function_exists('sql_fetch_assoc'))
\r
33 * Errors before the database connection has been made
\r
35 function startUpError($msg, $title) {
\r
37 <html xmlns="http://www.w3.org/1999/xhtml">
\r
38 <head><title><?php echo Entity::hsc($title)?></title></head>
\r
40 <h1><?php echo Entity::hsc($title)?></h1>
\r
48 * Connects to mysql server
\r
50 function sql_connect_args($mysql_host = 'localhost', $mysql_user = '', $mysql_password = '', $mysql_database = '') {
\r
51 global $MYSQL_HANDLER;
\r
54 if (i18n::strpos($mysql_host,':') === false) {
\r
55 $host = $mysql_host;
\r
60 list($host,$port) = preg_split("#:#", $mysql_host);
\r
63 $port = ';port='.trim($port);
\r
71 switch ($MYSQL_HANDLER[1]) {
\r
74 if (is_numeric($portnum)) $port = ':'.intval($portnum);
\r
76 $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password);
\r
79 if (is_numeric($portnum)) $port = ','.intval($portnum);
\r
81 $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password);
\r
84 if (is_numeric($portnum)) $port = ':'.intval($portnum);
\r
86 $DBH = new PDO($MYSQL_HANDLER[1].':dbname=//'.$host.$port.'/'.$mysql_database, $mysql_user, $mysql_password);
\r
89 if (is_numeric($portnum)) $port = ';PORT='.intval($portnum);
\r
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);
\r
95 if (is_numeric($portnum)) $port = ';port='.intval($portnum);
\r
97 $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password);
\r
101 if (is_numeric($portnum)) $port = ':'.intval($portnum);
\r
103 $DBH = new PDO($MYSQL_HANDLER[1].':'.$mysql_database, $mysql_user, $mysql_password);
\r
106 $DBH->sqliteCreateFunction('SUBSTRING', 'substr', 3);
\r
107 $DBH->sqliteCreateFunction('UNIX_TIMESTAMP', 'strtotime', 1);
\r
112 $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password);
\r
118 } catch (PDOException $e) {
\r
120 startUpError('<p>a1 Error!: ' . $e->getMessage() . '</p>', 'Connect Error');
\r
123 if ( $mysql_database )
\r
125 sql_set_charset('utf8');
\r
130 echo '<hr />DBH: '.print_r($SQL_DBH,true).'<hr />';
\r
131 $result = sql_query('SHOW VARIABLES LIKE \'char%\';');
\r
132 while(FALSE !== ($row = sql_fetch_row($result)))
\r
134 echo "{$row[0]}: {$row[1]}\n";
\r
142 * Connects to mysql server
\r
144 function sql_connect() {
\r
145 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN, $MYSQL_HANDLER, $SQL_DBH;
\r
148 if (i18n::strpos($MYSQL_HOST,':') === false) {
\r
149 $host = $MYSQL_HOST;
\r
154 list($host,$port) = preg_split("#:#", $MYSQL_HOST);
\r
155 if (isset($port)) {
\r
157 $port = ';port='.trim($port);
\r
165 switch ($MYSQL_HANDLER[1]) {
\r
168 if (is_numeric($portnum)) $port = ':'.intval($portnum);
\r
170 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
\r
173 if (is_numeric($portnum)) $port = ','.intval($portnum);
\r
175 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
\r
178 if (is_numeric($portnum)) $port = ':'.intval($portnum);
\r
180 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':dbname=//'.$host.$port.'/'.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
\r
183 if (is_numeric($portnum)) $port = ';PORT='.intval($portnum);
\r
185 $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);
\r
189 if (is_numeric($portnum)) $port = ';port='.intval($portnum);
\r
191 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
\r
195 if (is_numeric($portnum)) $port = ':'.intval($portnum);
\r
197 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':'.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
\r
200 $SQL_DBH->sqliteCreateFunction('SUBSTRING', 'substr', 3);
\r
201 $SQL_DBH->sqliteCreateFunction('UNIX_TIMESTAMP', 'strtotime', 1);
\r
206 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
\r
210 //$SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
\r
212 } catch (PDOException $e) {
\r
214 startUpError('<p>a2 Error!: ' . $e->getMessage() . '</p>', 'Connect Error');
\r
217 if ( $MYSQL_DATABASE )
\r
219 sql_set_charset('utf8');
\r
224 echo '<hr />DBH: '.print_r($SQL_DBH,true).'<hr />';
\r
225 $result = sql_query('SHOW VARIABLES LIKE \'char%\';');
\r
226 while(FALSE !== ($row = sql_fetch_row($result)))
\r
228 echo "{$row[0]}: {$row[1]}\n";
\r
232 $MYSQL_CONN =& $SQL_DBH;
\r
233 // echo '<hr />DBH: '.print_r($SQL_DBH,true).'<hr />';
\r
234 $MYSQL_CONN =& $SQL_DBH;
\r
240 * disconnects from SQL server
\r
242 function sql_disconnect(&$dbh=NULL) {
\r
244 if (is_null($dbh)) $SQL_DBH = NULL;
\r
250 * use sql_disconnect() instead of this
\r
253 * @param resource $conn resource of mySQL connection
\r
256 function sql_close(&$dbh=NULL)
\r
259 if ( is_null($dbh) )
\r
271 * executes an SQL query
\r
273 function sql_query($query,$dbh=NULL) {
\r
274 global $SQLCount,$SQL_DBH;
\r
276 //echo '<hr />SQL_DBH: ';
\r
277 //print_r($SQL_DBH);
\r
278 //echo '<hr />DBH: ';
\r
281 //echo $query.'<hr />';
\r
282 if (is_null($dbh)) $res = $SQL_DBH->query($query);
\r
283 else $res = $dbh->query($query);
\r
284 //if ($res->errorCode() != '00000') {
\r
285 // $errors = $res->errorInfo();
\r
286 if (($res === false) || ($res->errorCode() != '00000') ) {
\r
287 $errors = ($res !== false) ? $res->errorInfo() : ( is_object($dbh) ? $dbh->errorInfo() : $SQL_DBH->errorInfo());
\r
288 print("SQL error with query $query: " . $errors[0].'-'.$errors[1].' '.$errors[2] . '<p />');
\r
295 * executes an SQL error
\r
297 function sql_error($dbh=NULL)
\r
300 if (is_null($dbh)) $error = $SQL_DBH->errorInfo();
\r
301 else $error = $dbh->errorInfo();
\r
302 if ($error[0] != '00000') {
\r
303 return $error[0].'-'.$error[1].' '.$error[2];
\r
309 * executes an SQL db select
\r
311 function sql_select_db($db,&$dbh=NULL)
\r
313 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN, $MYSQL_HANDLER, $SQL_DBH;
\r
314 //echo '<hr />'.print_r($dbh,true).'<hr />';
\r
316 if (is_null($dbh)) {
\r
319 list($host,$port) = preg_split("#:#", $MYSQL_HOST);
\r
320 if (isset($port)) {
\r
322 $port = ';port='.trim($port);
\r
328 //$SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.trim($host).$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
\r
329 //$SQL_DBH = sql_connect();
\r
330 switch ($MYSQL_HANDLER[1]) {
\r
333 if (is_numeric($portnum)) $port = ':'.intval($portnum);
\r
335 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
\r
338 if (is_numeric($portnum)) $port = ','.intval($portnum);
\r
340 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
\r
343 if (is_numeric($portnum)) $port = ':'.intval($portnum);
\r
345 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':dbname=//'.$host.$port.'/'.$db, $MYSQL_USER, $MYSQL_PASSWORD);
\r
348 if (is_numeric($portnum)) $port = ';PORT='.intval($portnum);
\r
350 $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);
\r
354 if (is_numeric($portnum)) $port = ';port='.intval($portnum);
\r
356 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
\r
360 if (is_numeric($portnum)) $port = ':'.intval($portnum);
\r
362 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':'.$db, $MYSQL_USER, $MYSQL_PASSWORD);
\r
366 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
\r
370 } catch (PDOException $e) {
\r
371 startUpError('<p>a3 Error!: ' . $e->getMessage() . '</p>', 'Connect Error');
\r
376 if ($dbh->exec("USE $db") !== false) return 1;
\r
382 * executes an SQL real escape
\r
384 function sql_real_escape_string($val,$dbh=NULL)
\r
386 global $MYSQL_HANDLER;
\r
387 if (stripos($MYSQL_HANDLER[1] , 'sqlite') !== false)
\r
389 return sqlite_escape_string($val);
\r
393 return addslashes($val);
\r
398 * executes an PDO::quote() like escape, ie adds quotes arround the string and escapes chars as needed
\r
400 function sql_quote_string($val,$dbh=NULL) {
\r
403 return $SQL_DBH->quote($val);
\r
405 return $dbh->quote($val);
\r
409 * executes an SQL insert id
\r
411 function sql_insert_id($dbh=NULL)
\r
415 return $SQL_DBH->lastInsertId();
\r
417 return $dbh->lastInsertId();
\r
421 * executes an SQL result request
\r
423 function sql_result($res, $row = 0, $col = 0)
\r
425 $results = array();
\r
426 if (intval($row) < 1) {
\r
427 $results = $res->fetch(PDO::FETCH_BOTH);
\r
428 return $results[$col];
\r
431 for ($i = 0; $i < intval($row); $i++) {
\r
432 $results = $res->fetch(PDO::FETCH_BOTH);
\r
434 $results = $res->fetch(PDO::FETCH_BOTH);
\r
435 return $results[$col];
\r
440 * frees sql result resources
\r
442 function sql_free_result($res)
\r
449 * returns number of rows in SQL result
\r
451 function sql_num_rows($res)
\r
453 return $res->rowCount();
\r
457 * returns number of rows affected by SQL query
\r
459 function sql_affected_rows($res)
\r
461 return $res->rowCount();
\r
465 * Get number of fields in result
\r
467 function sql_num_fields($res)
\r
469 return $res->columnCount();
\r
473 * fetches next row of SQL result as an associative array
\r
475 function sql_fetch_assoc($res)
\r
477 $results = array();
\r
478 $results = $res->fetch(PDO::FETCH_ASSOC);
\r
483 * Fetch a result row as an associative array, a numeric array, or both
\r
485 function sql_fetch_array($res)
\r
487 $results = array();
\r
488 $results = $res->fetch(PDO::FETCH_BOTH);
\r
493 * fetches next row of SQL result as an object
\r
495 function sql_fetch_object($res)
\r
498 $results = $res->fetchObject();
\r
503 * Get a result row as an enumerated array
\r
505 function sql_fetch_row($res)
\r
507 $results = array();
\r
508 $results = $res->fetch(PDO::FETCH_NUM);
\r
513 * Get column information from a result and return as an object
\r
515 function sql_fetch_field($res,$offset = 0)
\r
517 $results = array();
\r
519 $results = $res->getColumnMeta($offset);
\r
520 foreach($results as $key=>$value) {
\r
521 $obj->$key = $value;
\r
527 * Get current system status (returns string)
\r
529 function sql_stat($dbh=NULL)
\r
540 * Returns the name of the character set
\r
542 function sql_client_encoding($dbh=NULL)
\r
553 * Get SQL client version
\r
555 function sql_get_client_info()
\r
558 return $SQL_DBH->getAttribute(constant("PDO::ATTR_CLIENT_VERSION"));
\r
562 * Get SQL server version
\r
564 function sql_get_server_info($dbh=NULL)
\r
568 return $SQL_DBH->getAttribute(constant("PDO::ATTR_SERVER_VERSION"));
\r
570 return $dbh->getAttribute(constant("PDO::ATTR_SERVER_VERSION"));
\r
574 * Returns a string describing the type of SQL connection in use for the connection or FALSE on failure
\r
576 function sql_get_host_info($dbh=NULL)
\r
580 return $SQL_DBH->getAttribute(constant("PDO::ATTR_SERVER_INFO"));
\r
582 return $dbh->getAttribute(constant("PDO::ATTR_SERVER_INFO"));
\r
586 * Returns the SQL protocol on success, or FALSE on failure.
\r
588 function sql_get_proto_info($dbh=NULL)
\r
599 * Get the name of the specified field in a result
\r
601 function sql_field_name($res, $offset = 0)
\r
603 $column = $res->getColumnMeta($offset);
\r
605 return $column['name'];
\r
611 * Set character encodings in each fields
\r
613 function sql_set_charset($charset)
\r
615 global $MYSQL_HANDLER, $SQL_DBH;
\r
619 if ( $MYSQL_HANDLER[0] == '' || in_array('mysql', $MYSQL_HANDLER) )
\r
623 * It's great help to you refering the same function in mysql.php.
\r
624 * PDO::MySQL has no function as same as mysql_set_charset().
\r
626 $charset = strtolower($charset);
\r
627 $mysql_version = preg_replace('#^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{1,2})(.*)$#', '$1.$2.$3', sql_get_server_info($SQL_DBH));
\r
629 if ( version_compare($mysql_version, '5.0.7', '<') )
\r
631 $result = sql_query("SET CHARACTER SET {$charset};");
\r
635 $result = sql_query("SET NAMES {$charset};");
\r
641 /**************************************************************************
\r
642 Unimplemented mysql_* functions
\r
644 # mysql_ data_ seek (maybe useful)
\r
645 # mysql_ errno (maybe useful)
\r
646 # mysql_ fetch_ lengths (maybe useful)
\r
647 # mysql_ field_ flags (maybe useful)
\r
648 # mysql_ field_ len (maybe useful)
\r
649 # mysql_ field_ seek (maybe useful)
\r
650 # mysql_ field_ table (maybe useful)
\r
651 # mysql_ field_ type (maybe useful)
\r
652 # mysql_ info (maybe useful)
\r
653 # mysql_ list_ processes (maybe useful)
\r
654 # mysql_ ping (maybe useful)
\r
655 # mysql_ set_ charset (maybe useful, requires php >=5.2.3 and mysql >=5.0.7)
\r
656 # mysql_ thread_ id (maybe useful)
\r
658 # mysql_ db_ name (useful only if working on multiple dbs which we do not do)
\r
659 # mysql_ list_ dbs (useful only if working on multiple dbs which we do not do)
\r
661 # mysql_ pconnect (probably not useful and could cause some unintended performance issues)
\r
662 # mysql_ unbuffered_ query (possibly useful, but complicated and not supported by all database drivers (pdo))
\r
664 # mysql_ change_ user (deprecated)
\r
665 # mysql_ create_ db (deprecated)
\r
666 # mysql_ db_ query (deprecated)
\r
667 # mysql_ drop_ db (deprecated)
\r
668 # mysql_ escape_ string (deprecated)
\r
669 # mysql_ list_ fields (deprecated)
\r
670 # mysql_ list_ tables (deprecated)
\r
671 # mysql_ tablename (deprecated)
\r
673 *******************************************************************/
\r