4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2012 The Nucleus Group
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)
13 * complete sql_* wrappers for mysql functions
15 * functions moved from globalfunctions.php: sql_connect, sql_disconnect, sql_query
22 if (!function_exists('sql_fetch_assoc'))
25 * Errors before the database connection has been made
27 function startUpError($msg, $title) {
28 if (!defined('_CHARSET')) {
29 define('_CHARSET', 'UTF-8');
31 if (!defined('_HTML_XML_NAME_SPACE_AND_LANG_CODE')) {
32 define('_HTML_XML_NAME_SPACE_AND_LANG_CODE', 'xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us"');
34 sendContentType('text/html','',_CHARSET);
36 <html <?php echo _HTML_XML_NAME_SPACE_AND_LANG_CODE; ?>>
37 <head><meta http-equiv="Content-Type" content="text/html; charset=<?php echo _CHARSET?>" />
38 <title><?php echo htmlspecialchars($title,ENT_QUOTES)?></title></head>
40 <h1><?php echo htmlspecialchars($title,ENT_QUOTES)?></h1>
49 * Connects to mysql server
51 function sql_connect_args($mysql_host = 'localhost', $mysql_user = '', $mysql_password = '', $mysql_database = '') {
52 global $MYSQL_HANDLER;
55 if (strpos($mysql_host,':') === false) {
61 list($host,$port) = explode(":",$mysql_host);
64 $port = ';port='.trim($port);
72 switch ($MYSQL_HANDLER[1]) {
75 if (is_numeric($portnum)) $port = ':'.intval($portnum);
77 $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password);
80 if (is_numeric($portnum)) $port = ','.intval($portnum);
82 $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password);
85 if (is_numeric($portnum)) $port = ':'.intval($portnum);
87 $DBH = new PDO($MYSQL_HANDLER[1].':dbname=//'.$host.$port.'/'.$mysql_database, $mysql_user, $mysql_password);
90 if (is_numeric($portnum)) $port = ';PORT='.intval($portnum);
92 $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);
96 if (is_numeric($portnum)) $port = ';port='.intval($portnum);
98 $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password);
102 if (is_numeric($portnum)) $port = ':'.intval($portnum);
104 $DBH = new PDO($MYSQL_HANDLER[1].':'.$mysql_database, $mysql_user, $mysql_password);
108 $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password);
114 } catch (PDOException $e) {
116 startUpError('<p>a1 Error!: ' . $e->getMessage() . '</p>', 'Connect Error');
118 //echo '<hr />DBH: '.print_r($DBH,true).'<hr />';
123 * Connects to mysql server
125 function sql_connect() {
126 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN, $MYSQL_HANDLER, $SQL_DBH;
129 if (strpos($MYSQL_HOST,':') === false) {
134 list($host,$port) = explode(":",$MYSQL_HOST);
137 $port = ';port='.trim($port);
145 switch ($MYSQL_HANDLER[1]) {
148 if (is_numeric($portnum)) $port = ':'.intval($portnum);
150 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
153 if (is_numeric($portnum)) $port = ','.intval($portnum);
155 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
158 if (is_numeric($portnum)) $port = ':'.intval($portnum);
160 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':dbname=//'.$host.$port.'/'.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
163 if (is_numeric($portnum)) $port = ';PORT='.intval($portnum);
165 $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);
169 if (is_numeric($portnum)) $port = ';port='.intval($portnum);
171 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
175 if (is_numeric($portnum)) $port = ':'.intval($portnum);
177 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':'.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
181 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
185 //$SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
187 // <add for garble measure>
188 if (strpos($MYSQL_HANDLER[1], 'mysql') === 0) {
189 if (defined('_CHARSET')){
192 $resource = $SQL_DBH->query("show variables LIKE 'character_set_database'");
193 $resource->bindColumn('Value', $charset);
194 $resource->fetchAll();
195 // in trouble of encoding,uncomment the following line.
196 // $charset = "ujis";
197 // $charset = "utf8";
199 sql_set_charset_jp($charset);
201 // </add for garble measure>*/
202 } catch (PDOException $e) {
204 startUpError('<p>a2 Error!: ' . $e->getMessage() . '</p>', 'Connect Error');
206 // echo '<hr />DBH: '.print_r($SQL_DBH,true).'<hr />';
207 $MYSQL_CONN &= $SQL_DBH;
213 * disconnects from SQL server
215 function sql_disconnect(&$dbh=NULL) {
217 if (is_null($dbh)) $SQL_DBH = NULL;
221 function sql_close(&$dbh=NULL) {
223 if (is_null($dbh)) $SQL_DBH = NULL;
228 * executes an SQL query
230 function sql_query($query,$dbh=NULL) {
231 global $SQLCount,$SQL_DBH;
233 //echo '<hr />SQL_DBH: ';
235 //echo '<hr />DBH: ';
238 //echo $query.'<hr />';
239 if (is_null($dbh)) $res = $SQL_DBH->query($query);
240 else $res = $dbh->query($query);
241 if ($res->errorCode() != '00000') {
242 $errors = $res->errorInfo();
243 print("SQL error with query $query: " . $errors[0].'-'.$errors[1].' '.$errors[2] . '<p />');
250 * executes an SQL error
252 function sql_error($dbh=NULL)
255 if (is_null($dbh)) $error = $SQL_DBH->errorInfo();
256 else $error = $dbh->errorInfo();
257 if ($error[0] != '00000') {
258 return $error[0].'-'.$error[1].' '.$error[2];
264 * executes an SQL db select
266 function sql_select_db($db,&$dbh=NULL)
268 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN, $MYSQL_HANDLER, $SQL_DBH;
269 //echo '<hr />'.print_r($dbh,true).'<hr />';
274 list($host,$port) = explode(":",$MYSQL_HOST);
277 $port = ';port='.trim($port);
283 //$SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.trim($host).$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
284 //$SQL_DBH = sql_connect();
285 switch ($MYSQL_HANDLER[1]) {
288 if (is_numeric($portnum)) $port = ':'.intval($portnum);
290 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
293 if (is_numeric($portnum)) $port = ','.intval($portnum);
295 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
298 if (is_numeric($portnum)) $port = ':'.intval($portnum);
300 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':dbname=//'.$host.$port.'/'.$db, $MYSQL_USER, $MYSQL_PASSWORD);
303 if (is_numeric($portnum)) $port = ';PORT='.intval($portnum);
305 $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);
309 if (is_numeric($portnum)) $port = ';port='.intval($portnum);
311 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
315 if (is_numeric($portnum)) $port = ':'.intval($portnum);
317 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':'.$db, $MYSQL_USER, $MYSQL_PASSWORD);
321 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
325 } catch (PDOException $e) {
326 startUpError('<p>a3 Error!: ' . $e->getMessage() . '</p>', 'Connect Error');
331 if ($dbh->exec("USE $db") !== false) return 1;
337 * executes an SQL real escape
339 function sql_real_escape_string($val,$dbh=NULL)
341 return addslashes($val);
345 * executes an PDO::quote() like escape, ie adds quotes arround the string and escapes chars as needed
347 function sql_quote_string($val,$dbh=NULL) {
350 return $SQL_DBH->quote($val);
352 return $dbh->quote($val);
356 * executes an SQL insert id
358 function sql_insert_id($dbh=NULL)
362 return $SQL_DBH->lastInsertId();
364 return $dbh->lastInsertId();
368 * executes an SQL result request
370 function sql_result($res, $row = 0, $col = 0)
373 if (intval($row) < 1) {
374 $results = $res->fetch(PDO::FETCH_BOTH);
375 return $results[$col];
378 for ($i = 0; $i < intval($row); $i++) {
379 $results = $res->fetch(PDO::FETCH_BOTH);
381 $results = $res->fetch(PDO::FETCH_BOTH);
382 return $results[$col];
387 * frees sql result resources
389 function sql_free_result($res)
396 * returns number of rows in SQL result
398 function sql_num_rows($res)
400 return $res->rowCount();
404 * returns number of rows affected by SQL query
406 function sql_affected_rows($res)
408 return $res->rowCount();
412 * Get number of fields in result
414 function sql_num_fields($res)
416 return $res->columnCount();
420 * fetches next row of SQL result as an associative array
422 function sql_fetch_assoc($res)
425 $results = $res->fetch(PDO::FETCH_ASSOC);
430 * Fetch a result row as an associative array, a numeric array, or both
432 function sql_fetch_array($res)
435 $results = $res->fetch(PDO::FETCH_BOTH);
440 * fetches next row of SQL result as an object
442 function sql_fetch_object($res)
445 $results = $res->fetchObject();
450 * Get a result row as an enumerated array
452 function sql_fetch_row($res)
455 $results = $res->fetch(PDO::FETCH_NUM);
460 * Get column information from a result and return as an object
462 function sql_fetch_field($res,$offset = 0)
466 $results = $res->getColumnMeta($offset);
467 foreach($results as $key=>$value) {
474 * Get current system status (returns string)
476 function sql_stat($dbh=NULL)
487 * Returns the name of the character set
489 function sql_client_encoding($dbh=NULL)
500 * Get SQL client version
502 function sql_get_client_info()
505 return $SQL_DBH->getAttribute(constant("PDO::ATTR_CLIENT_VERSION"));
509 * Get SQL server version
511 function sql_get_server_info($dbh=NULL)
515 return $SQL_DBH->getAttribute(constant("PDO::ATTR_SERVER_VERSION"));
517 return $dbh->getAttribute(constant("PDO::ATTR_SERVER_VERSION"));
521 * Returns a string describing the type of SQL connection in use for the connection or FALSE on failure
523 function sql_get_host_info($dbh=NULL)
527 return $SQL_DBH->getAttribute(constant("PDO::ATTR_SERVER_INFO"));
529 return $dbh->getAttribute(constant("PDO::ATTR_SERVER_INFO"));
533 * Returns the SQL protocol on success, or FALSE on failure.
535 function sql_get_proto_info($dbh=NULL)
546 * Get the name of the specified field in a result
548 function sql_field_name($res, $offset = 0)
550 $column = $res->getColumnMeta($offset);
552 return $column['name'];
557 /**************************************************************************
558 Unimplemented mysql_* functions
560 # mysql_ data_ seek (maybe useful)
561 # mysql_ errno (maybe useful)
562 # mysql_ fetch_ lengths (maybe useful)
563 # mysql_ field_ flags (maybe useful)
564 # mysql_ field_ len (maybe useful)
565 # mysql_ field_ name (maybe useful)
566 # mysql_ field_ seek (maybe useful)
567 # mysql_ field_ table (maybe useful)
568 # mysql_ field_ type (maybe useful)
569 # mysql_ info (maybe useful)
570 # mysql_ list_ processes (maybe useful)
571 # mysql_ ping (maybe useful)
572 # mysql_ set_ charset (maybe useful, requires php >=5.2.3 and mysql >=5.0.7)
573 # mysql_ thread_ id (maybe useful)
575 # mysql_ db_ name (useful only if working on multiple dbs which we do not do)
576 # mysql_ list_ dbs (useful only if working on multiple dbs which we do not do)
578 # mysql_ pconnect (probably not useful and could cause some unintended performance issues)
579 # mysql_ unbuffered_ query (possibly useful, but complicated and not supported by all database drivers (pdo))
581 # mysql_ change_ user (deprecated)
582 # mysql_ create_ db (deprecated)
583 # mysql_ db_ query (deprecated)
584 # mysql_ drop_ db (deprecated)
585 # mysql_ escape_ string (deprecated)
586 # mysql_ list_ fields (deprecated)
587 # mysql_ list_ tables (deprecated)
588 # mysql_ tablename (deprecated)
590 *******************************************************************/
593 * for preventing I/O strings from auto-detecting the charactor encodings by MySQL
595 * Jan.20, 2011 by kotorisan and cacher
596 * refering to their conversation below,
597 * http://japan.nucleuscms.org/bb/viewtopic.php?p=26581
599 * NOTE: shift_jis is only supported for output. Using shift_jis in DB is prohibited.
600 * NOTE: iso-8859-x,windows-125x if _CHARSET is unset.
602 function sql_set_charset_jp($charset) {
603 global $MYSQL_HANDLER,$SQL_DBH;
604 if (strpos($MYSQL_HANDLER[1], 'mysql') === 0) {
605 switch(strtolower($charset)){
\r
615 $charset = 'gb2312';
\r
624 $charset = 'latin1';
\r
627 $mySqlVer = implode('.', array_map('intval', explode('.', sql_get_server_info())));
628 if (version_compare($mySqlVer, '4.1.0', '>=')) {
629 $res = $SQL_DBH->exec("SET CHARACTER SET " . $charset);