4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2007 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-2007 The Nucleus Group
16 * @version $Id: mysql.php 1279 2008-10-23 08:18:26Z shizuki $
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) {
37 <html xmlns="http://www.w3.org/1999/xhtml">
38 <head><title><?php echo htmlspecialchars($title)?></title></head>
40 <h1><?php echo htmlspecialchars($title)?></h1>
48 * Connects to mysql server
50 function sql_connect_args($mysql_host = 'localhost', $mysql_user = '', $mysql_password = '', $mysql_database = '') {
51 global $MYSQL_HANDLER;
54 if (strpos($mysql_host,':') === false) {
59 list($host,$port) = explode(":",$mysql_host);
61 $port = ';port='.trim($port);
66 $DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$mysql_database, $mysql_user, $mysql_password);
68 } catch (PDOException $e) {
70 startUpError('<p>a1 Error!: ' . $e->getMessage() . '</p>', 'Connect Error');
72 //echo '<hr />DBH: '.print_r($DBH,true).'<hr />';
77 * Connects to mysql server
79 function sql_connect() {
80 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN, $MYSQL_HANDLER, $SQL_DBH;
83 if (strpos($MYSQL_HOST,':') === false) {
88 list($host,$port) = explode(":",$MYSQL_HOST);
90 $port = ';port='.trim($port);
95 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.$host.$port.';dbname='.$MYSQL_DATABASE, $MYSQL_USER, $MYSQL_PASSWORD);
96 /*/ <add for garble measure>
97 if (strpos($MYSQL_HANDLER[1], 'mysql') === 0) {
98 $resource = $SQL_DBH->query("show variables LIKE 'character_set_database'");
99 $resource->bindColumn('Value', $charset);
100 $resource->fetchAll();
101 $SQL_DBH->exec("SET CHARACTER SET " . $charset);
103 // </add for garble measure>*/
104 } catch (PDOException $e) {
106 startUpError('<p>a2 Error!: ' . $e->getMessage() . '</p>', 'Connect Error');
108 // echo '<hr />DBH: '.print_r($SQL_DBH,true).'<hr />';
109 $MYSQL_CONN &= $SQL_DBH;
115 * disconnects from SQL server
117 function sql_disconnect(&$dbh=NULL) {
119 if (is_null($dbh)) $SQL_DBH = NULL;
123 function sql_close(&$dbh=NULL) {
125 if (is_null($dbh)) $SQL_DBH = NULL;
130 * executes an SQL query
132 function sql_query($query,$dbh=NULL) {
133 global $SQLCount,$SQL_DBH;
135 //echo '<hr />SQL_DBH: ';
137 //echo '<hr />DBH: ';
140 //echo $query.'<hr />';
141 if (is_null($dbh)) $res = $SQL_DBH->query($query);
142 else $res = $dbh->query($query);
143 if ($res->errorCode() != '00000') {
144 $errors = $res->errorInfo();
145 print("SQL error with query $query: " . $errors[0].'-'.$errors[1].' '.$errors[2] . '<p />');
152 * executes an SQL error
154 function sql_error($dbh=NULL)
157 if (is_null($dbh)) $error = $SQL_DBH->errorInfo();
158 else $error = $dbh->errorInfo();
159 if ($error[0] != '00000') {
160 return $error[0].'-'.$error[1].' '.$error[2];
166 * executes an SQL db select
168 function sql_select_db($db,&$dbh=NULL)
170 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN, $MYSQL_HANDLER, $SQL_DBH;
171 //echo '<hr />'.print_r($dbh,true).'<hr />';
176 list($host,$port) = explode(":",$MYSQL_HOST);
178 $port = ';port='.trim($port);
181 $SQL_DBH = new PDO($MYSQL_HANDLER[1].':host='.trim($host).$port.';dbname='.$db, $MYSQL_USER, $MYSQL_PASSWORD);
183 } catch (PDOException $e) {
184 startUpError('<p>a3 Error!: ' . $e->getMessage() . '</p>', 'Connect Error');
189 if ($dbh->exec("USE $db") !== false) return 1;
195 * executes an SQL real escape
197 function sql_real_escape_string($val,$dbh=NULL)
199 return addslashes($val);
203 * executes an PDO::quote() like escape, ie adds quotes arround the string and escapes chars as needed
205 function sql_quote_string($val,$dbh=NULL) {
208 return $SQL_DBH->quote($val);
210 return $dbh->quote($val);
214 * executes an SQL insert id
216 function sql_insert_id($dbh=NULL)
220 return $SQL_DBH->lastInsertId();
222 return $dbh->lastInsertId();
226 * executes an SQL result request
228 function sql_result($res, $row = 0, $col = 0)
231 if (intval($row) < 1) {
232 $results = $res->fetch(PDO::FETCH_BOTH);
233 return $results[$col];
236 for ($i = 0; $i < intval($row); $i++) {
237 $results = $res->fetch(PDO::FETCH_BOTH);
239 $results = $res->fetch(PDO::FETCH_BOTH);
240 return $results[$col];
245 * frees sql result resources
247 function sql_free_result($res)
254 * returns number of rows in SQL result
256 function sql_num_rows($res)
258 return $res->rowCount();
262 * returns number of rows affected by SQL query
264 function sql_affected_rows($res)
266 return $res->rowCount();
270 * Get number of fields in result
272 function sql_num_fields($res)
274 return $res->columnCount();
278 * fetches next row of SQL result as an associative array
280 function sql_fetch_assoc($res)
283 $results = $res->fetch(PDO::FETCH_ASSOC);
288 * Fetch a result row as an associative array, a numeric array, or both
290 function sql_fetch_array($res)
293 $results = $res->fetch(PDO::FETCH_BOTH);
298 * fetches next row of SQL result as an object
300 function sql_fetch_object($res)
303 $results = $res->fetchObject();
308 * Get a result row as an enumerated array
310 function sql_fetch_row($res)
313 $results = $res->fetch(PDO::FETCH_NUM);
318 * Get column information from a result and return as an object
320 function sql_fetch_field($res,$offset = 0)
324 $results = $res->getColumnMeta($offset);
325 foreach($results as $key=>$value) {
332 * Get current system status (returns string)
334 function sql_stat($dbh=NULL)
345 * Returns the name of the character set
347 function sql_client_encoding($dbh=NULL)
358 * Get SQL client version
360 function sql_get_client_info()
363 return $SQL_DBH->getAttribute(constant("PDO::ATTR_CLIENT_VERSION"));
367 * Get SQL server version
369 function sql_get_server_info($dbh=NULL)
373 return $SQL_DBH->getAttribute(constant("PDO::ATTR_SERVER_VERSION"));
375 return $dbh->getAttribute(constant("PDO::ATTR_SERVER_VERSION"));
379 * Returns a string describing the type of SQL connection in use for the connection or FALSE on failure
381 function sql_get_host_info($dbh=NULL)
385 return $SQL_DBH->getAttribute(constant("PDO::ATTR_SERVER_INFO"));
387 return $dbh->getAttribute(constant("PDO::ATTR_SERVER_INFO"));
391 * Returns the SQL protocol on success, or FALSE on failure.
393 function sql_get_proto_info($dbh=NULL)
406 /**************************************************************************
407 Unimplemented mysql_* functions
409 # mysql_ change_ user
416 # mysql_ escape_ string
417 # mysql_ fetch_ lengths
418 # mysql_ field_ flags
422 # mysql_ field_ table
426 # mysql_ list_ fields
427 # mysql_ list_ processes
428 # mysql_ list_ tables
431 # mysql_ set_ charset
434 # mysql_ unbuffered_ query
435 *******************************************************************/