4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2011 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)
14 * @license http://nucleuscms.org/license.txt GNU General Public License
15 * @copyright Copyright (C) 2002-2011 The Nucleus Group
20 * complete sql_* wrappers for mysql functions
22 * functions moved from globalfunctions.php: sql_connect, sql_disconnect, sql_query
30 if (!function_exists('sql_fetch_assoc'))
33 * Errors before the database connection has been made
35 function startUpError($msg, $title) {
36 if (!defined('_CHARSET')) {
37 define('_CHARSET', 'UTF-8');
39 if (!defined('_HTML_XML_NAME_SPACE_AND_LANG_CODE')) {
40 define('_HTML_XML_NAME_SPACE_AND_LANG_CODE', 'xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us"');
42 sendContentType('text/html','',_CHARSET);
44 <html <?php echo _HTML_XML_NAME_SPACE_AND_LANG_CODE; ?>>
45 <head><meta http-equiv="Content-Type" content="text/html; charset=<?php echo _CHARSET?>" />
46 <title><?php echo htmlspecialchars($title,ENT_QUOTES)?></title></head>
48 <h1><?php echo htmlspecialchars($title,ENT_QUOTES)?></h1>
57 * Connects to mysql server
59 function sql_connect_args($mysql_host = 'localhost', $mysql_user = '', $mysql_password = '', $mysql_database = '') {
60 global $MYSQL_HANDLER;
63 if (strpos($mysql_host,':') === false) {
69 list($host,$port) = explode(":",$mysql_host);
72 $port = ';port='.trim($port);
80 switch ($MYSQL_HANDLER[1]) {
83 if (is_numeric($portnum)) $port = ':'.intval($portnum);
85 $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password);
88 if (is_numeric($portnum)) $port = ','.intval($portnum);
90 $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password);
93 if (is_numeric($portnum)) $port = ':'.intval($portnum);
95 $DBH = new PDO($MYSQL_HANDLER[1].':dbname=//'.$host.$port.'/'.$mysql_database, $mysql_user, $mysql_password);
98 if (is_numeric($portnum)) $port = ';PORT='.intval($portnum);
100 $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);
104 if (is_numeric($portnum)) $port = ';port='.intval($portnum);
106 $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password);
110 if (is_numeric($portnum)) $port = ':'.intval($portnum);
112 $DBH = new PDO($MYSQL_HANDLER[1].':'.$mysql_database, $mysql_user, $mysql_password);
116 $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password);
122 } catch (PDOException $e) {
124 startUpError('<p>a1 Error!: ' . $e->getMessage() . '</p>', 'Connect Error');
126 //echo '<hr />DBH: '.print_r($DBH,true).'<hr />';
131 * Connects to mysql server
133 function sql_connect() {
134 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN, $MYSQL_HANDLER, $SQL_DBH;
137 if (strpos($MYSQL_HOST,':') === false) {
142 list($host,$port) = explode(":",$MYSQL_HOST);
145 $port = ';port='.trim($port);
153 switch ($MYSQL_HANDLER[1]) {
156 if (is_numeric($portnum)) $port = ':'.intval($portnum);
158 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
161 if (is_numeric($portnum)) $port = ','.intval($portnum);
163 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
166 if (is_numeric($portnum)) $port = ':'.intval($portnum);
168 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':dbname=//'.$host.$port.'/'.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
171 if (is_numeric($portnum)) $port = ';PORT='.intval($portnum);
173 $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);
177 if (is_numeric($portnum)) $port = ';port='.intval($portnum);
179 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
183 if (is_numeric($portnum)) $port = ':'.intval($portnum);
185 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':'.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
189 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
193 //$SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
195 // <add for garble measure>
196 if (strpos($MYSQL_HANDLER[1], 'mysql') === 0) {
197 $resource = $SQL_DBH->query("show variables LIKE 'character_set_database'");
198 $resource->bindColumn('Value', $charset);
199 $resource->fetchAll();
200 $SQL_DBH->exec("SET CHARACTER SET " . $charset);
202 // </add for garble measure>*/
203 } catch (PDOException $e) {
205 startUpError('<p>a2 Error!: ' . $e->getMessage() . '</p>', 'Connect Error');
207 // echo '<hr />DBH: '.print_r($SQL_DBH,true).'<hr />';
208 $MYSQL_CONN &= $SQL_DBH;
214 * disconnects from SQL server
216 function sql_disconnect(&$dbh=NULL) {
218 if (is_null($dbh)) $SQL_DBH = NULL;
222 function sql_close(&$dbh=NULL) {
224 if (is_null($dbh)) $SQL_DBH = NULL;
229 * executes an SQL query
231 function sql_query($query,$dbh=NULL) {
232 global $SQLCount,$SQL_DBH;
234 //echo '<hr />SQL_DBH: ';
236 //echo '<hr />DBH: ';
239 //echo $query.'<hr />';
240 if (is_null($dbh)) $res = $SQL_DBH->query($query);
241 else $res = $dbh->query($query);
242 if ($res->errorCode() != '00000') {
243 $errors = $res->errorInfo();
244 print("SQL error with query $query: " . $errors[0].'-'.$errors[1].' '.$errors[2] . '<p />');
251 * executes an SQL error
253 function sql_error($dbh=NULL)
256 if (is_null($dbh)) $error = $SQL_DBH->errorInfo();
257 else $error = $dbh->errorInfo();
258 if ($error[0] != '00000') {
259 return $error[0].'-'.$error[1].' '.$error[2];
265 * executes an SQL db select
267 function sql_select_db($db,&$dbh=NULL)
269 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN, $MYSQL_HANDLER, $SQL_DBH;
270 //echo '<hr />'.print_r($dbh,true).'<hr />';
275 list($host,$port) = explode(":",$MYSQL_HOST);
278 $port = ';port='.trim($port);
284 //$SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.trim($host).$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
285 //$SQL_DBH = sql_connect();
286 switch ($MYSQL_HANDLER[1]) {
289 if (is_numeric($portnum)) $port = ':'.intval($portnum);
291 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
294 if (is_numeric($portnum)) $port = ','.intval($portnum);
296 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
299 if (is_numeric($portnum)) $port = ':'.intval($portnum);
301 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':dbname=//'.$host.$port.'/'.$db, $MYSQL_USER, $MYSQL_PASSWORD);
304 if (is_numeric($portnum)) $port = ';PORT='.intval($portnum);
306 $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);
310 if (is_numeric($portnum)) $port = ';port='.intval($portnum);
312 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
316 if (is_numeric($portnum)) $port = ':'.intval($portnum);
318 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':'.$db, $MYSQL_USER, $MYSQL_PASSWORD);
322 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
326 } catch (PDOException $e) {
327 startUpError('<p>a3 Error!: ' . $e->getMessage() . '</p>', 'Connect Error');
332 if ($dbh->exec("USE $db") !== false) return 1;
338 * executes an SQL real escape
340 function sql_real_escape_string($val,$dbh=NULL)
342 return addslashes($val);
346 * executes an PDO::quote() like escape, ie adds quotes arround the string and escapes chars as needed
348 function sql_quote_string($val,$dbh=NULL) {
351 return $SQL_DBH->quote($val);
353 return $dbh->quote($val);
357 * executes an SQL insert id
359 function sql_insert_id($dbh=NULL)
363 return $SQL_DBH->lastInsertId();
365 return $dbh->lastInsertId();
369 * executes an SQL result request
371 function sql_result($res, $row = 0, $col = 0)
374 if (intval($row) < 1) {
375 $results = $res->fetch(PDO::FETCH_BOTH);
376 return $results[$col];
379 for ($i = 0; $i < intval($row); $i++) {
380 $results = $res->fetch(PDO::FETCH_BOTH);
382 $results = $res->fetch(PDO::FETCH_BOTH);
383 return $results[$col];
388 * frees sql result resources
390 function sql_free_result($res)
397 * returns number of rows in SQL result
399 function sql_num_rows($res)
401 return $res->rowCount();
405 * returns number of rows affected by SQL query
407 function sql_affected_rows($res)
409 return $res->rowCount();
413 * Get number of fields in result
415 function sql_num_fields($res)
417 return $res->columnCount();
421 * fetches next row of SQL result as an associative array
423 function sql_fetch_assoc($res)
426 $results = $res->fetch(PDO::FETCH_ASSOC);
431 * Fetch a result row as an associative array, a numeric array, or both
433 function sql_fetch_array($res)
436 $results = $res->fetch(PDO::FETCH_BOTH);
441 * fetches next row of SQL result as an object
443 function sql_fetch_object($res)
446 $results = $res->fetchObject();
451 * Get a result row as an enumerated array
453 function sql_fetch_row($res)
456 $results = $res->fetch(PDO::FETCH_NUM);
461 * Get column information from a result and return as an object
463 function sql_fetch_field($res,$offset = 0)
467 $results = $res->getColumnMeta($offset);
468 foreach($results as $key=>$value) {
475 * Get current system status (returns string)
477 function sql_stat($dbh=NULL)
488 * Returns the name of the character set
490 function sql_client_encoding($dbh=NULL)
501 * Get SQL client version
503 function sql_get_client_info()
506 return $SQL_DBH->getAttribute(constant("PDO::ATTR_CLIENT_VERSION"));
510 * Get SQL server version
512 function sql_get_server_info($dbh=NULL)
516 return $SQL_DBH->getAttribute(constant("PDO::ATTR_SERVER_VERSION"));
518 return $dbh->getAttribute(constant("PDO::ATTR_SERVER_VERSION"));
522 * Returns a string describing the type of SQL connection in use for the connection or FALSE on failure
524 function sql_get_host_info($dbh=NULL)
528 return $SQL_DBH->getAttribute(constant("PDO::ATTR_SERVER_INFO"));
530 return $dbh->getAttribute(constant("PDO::ATTR_SERVER_INFO"));
534 * Returns the SQL protocol on success, or FALSE on failure.
536 function sql_get_proto_info($dbh=NULL)
547 * Get the name of the specified field in a result
549 function sql_field_name($res, $offset = 0)
551 $column = $res->getColumnMeta($offset);
553 return $column['name'];
558 /**************************************************************************
559 Unimplemented mysql_* functions
561 # mysql_ data_ seek (maybe useful)
562 # mysql_ errno (maybe useful)
563 # mysql_ fetch_ lengths (maybe useful)
564 # mysql_ field_ flags (maybe useful)
565 # mysql_ field_ len (maybe useful)
566 # mysql_ field_ name (maybe useful)
567 # mysql_ field_ seek (maybe useful)
568 # mysql_ field_ table (maybe useful)
569 # mysql_ field_ type (maybe useful)
570 # mysql_ info (maybe useful)
571 # mysql_ list_ processes (maybe useful)
572 # mysql_ ping (maybe useful)
573 # mysql_ set_ charset (maybe useful, requires php >=5.2.3 and mysql >=5.0.7)
574 # mysql_ thread_ id (maybe useful)
576 # mysql_ db_ name (useful only if working on multiple dbs which we do not do)
577 # mysql_ list_ dbs (useful only if working on multiple dbs which we do not do)
579 # mysql_ pconnect (probably not useful and could cause some unintended performance issues)
580 # mysql_ unbuffered_ query (possibly useful, but complicated and not supported by all database drivers (pdo))
582 # mysql_ change_ user (deprecated)
583 # mysql_ create_ db (deprecated)
584 # mysql_ db_ query (deprecated)
585 # mysql_ drop_ db (deprecated)
586 # mysql_ escape_ string (deprecated)
587 # mysql_ list_ fields (deprecated)
588 # mysql_ list_ tables (deprecated)
589 # mysql_ tablename (deprecated)
591 *******************************************************************/
594 * for JP installer only
596 function at_sql_query($query,$dbh=NULL) {
597 global $SQLCount,$SQL_DBH;
599 if (is_null($dbh)) $res = $SQL_DBH->query($query);
600 else $res = $dbh->query($query);
601 /* if ($res->errorCode() != '00000') {
602 $errors = $res->errorInfo();
603 print("SQL error with query $query: " . $errors[0].'-'.$errors[1].' '.$errors[2] . '<p />');