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
28 if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
31 *Errors before the database connection has been made
33 function startUpError($msg, $title) {
34 if (!defined('_CHARSET')) {
35 define('_CHARSET', 'UTF-8');
37 if (!defined('_HTML_XML_NAME_SPACE_AND_LANG_CODE')) {
38 define('_HTML_XML_NAME_SPACE_AND_LANG_CODE', 'xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us"');
40 sendContentType('text/html','',_CHARSET);
42 <html <?php echo _HTML_XML_NAME_SPACE_AND_LANG_CODE; ?>>
43 <head><meta http-equiv="Content-Type" content="text/html; charset=<?php echo _CHARSET?>" />
44 <title><?php echo htmlspecialchars($title,ENT_QUOTES)?></title></head>
46 <h1><?php echo htmlspecialchars($title,ENT_QUOTES)?></h1>
55 * Connects to mysql server with arguments
57 function sql_connect_args($mysql_host = 'localhost', $mysql_user = '', $mysql_password = '', $mysql_database = '') {
59 $CONN = @mysql_connect($mysql_host, $mysql_user, $mysql_password);
60 if ($mysql_database) mysql_select_db($mysql_database,$CONN);
66 * Connects to mysql server
68 function sql_connect() {
69 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN;
71 $MYSQL_CONN = @mysql_connect($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD) or startUpError('<p>Could not connect to MySQL database.</p>', 'Connect Error');
72 mysql_select_db($MYSQL_DATABASE) or startUpError('<p>Could not select database: ' . mysql_error() . '</p>', 'Connect Error');
75 // <add for garble measure>
76 $resource = sql_query("show variables LIKE 'character_set_database'");
77 $fetchDat = sql_fetch_assoc($resource);
78 $charset = $fetchDat['Value'];
79 $mySqlVer = implode('.', array_map('intval', explode('.', sql_get_server_info($MYSQL_CONN))));
80 if (version_compare($mySqlVer, '5.0.7', '>=') && function_exists('mysql_set_charset')) {
81 mysql_set_charset($charset);
82 } elseif (version_compare($mySqlVer, '4.1.0', '>=')) {
83 sql_query("SET CHARACTER SET " . $charset);
85 // </add for garble measure>
91 * disconnects from SQL server
93 function sql_disconnect($conn = false) {
95 if (!$conn) $conn = $MYSQL_CONN;
99 function sql_close($conn = false) {
101 if (!$conn) $conn = $MYSQL_CONN;
106 * executes an SQL query
108 function sql_query($query, $conn = false) {
109 global $SQLCount,$MYSQL_CONN;
110 if (!$conn) $conn = $MYSQL_CONN;
112 $res = mysql_query($query,$conn) or print("mySQL error with query $query: " . mysql_error($conn) . '<p />');
117 * executes an SQL error
119 function sql_error($conn = false)
122 if (!$conn) $conn = $MYSQL_CONN;
123 return mysql_error($conn);
127 * executes an SQL db select
129 function sql_select_db($db,$conn = false)
132 if (!$conn) $conn = $MYSQL_CONN;
133 return mysql_select_db($db,$conn);
137 * executes an SQL real escape
139 function sql_real_escape_string($val,$conn = false)
142 if (!$conn) $conn = $MYSQL_CONN;
143 return mysql_real_escape_string($val,$conn);
147 * executes an PDO::quote() like escape, ie adds quotes arround the string and escapes chars as needed
149 function sql_quote_string($val,$conn = false) {
151 if (!$conn) $conn = $MYSQL_CONN;
152 return "'".mysql_real_escape_string($val,$conn)."'";
156 * executes an SQL insert id
158 function sql_insert_id($conn = false)
161 if (!$conn) $conn = $MYSQL_CONN;
162 return mysql_insert_id($conn);
166 * executes an SQL result request
168 function sql_result($res, $row, $col)
170 return mysql_result($res,$row,$col);
174 * frees sql result resources
176 function sql_free_result($res)
178 return mysql_free_result($res);
182 * returns number of rows in SQL result
184 function sql_num_rows($res)
186 return mysql_num_rows($res);
190 * returns number of rows affected by SQL query
192 function sql_affected_rows($conn = false)
195 if (!$conn) $conn = $MYSQL_CONN;
196 return mysql_affected_rows($conn);
200 * Get number of fields in result
202 function sql_num_fields($res)
204 return mysql_num_fields($res);
208 * fetches next row of SQL result as an associative array
210 function sql_fetch_assoc($res)
212 return mysql_fetch_assoc($res);
216 * Fetch a result row as an associative array, a numeric array, or both
218 function sql_fetch_array($res)
220 return mysql_fetch_array($res);
224 * fetches next row of SQL result as an object
226 function sql_fetch_object($res)
228 return mysql_fetch_object($res);
232 * Get a result row as an enumerated array
234 function sql_fetch_row($res)
236 return mysql_fetch_row($res);
240 * Get column information from a result and return as an object
242 function sql_fetch_field($res,$offset = 0)
244 return mysql_fetch_field($res,$offset);
248 * Get current system status (returns string)
250 function sql_stat($conn=false)
253 if (!$conn) $conn = $MYSQL_CONN;
254 return mysql_stat($conn);
258 * Returns the name of the character set
260 function sql_client_encoding($conn=false)
263 if (!$conn) $conn = $MYSQL_CONN;
264 return mysql_client_encoding($conn);
268 * Get SQL client version
270 function sql_get_client_info()
272 return mysql_get_client_info();
276 * Get SQL server version
278 function sql_get_server_info($conn=false)
281 if (!$conn) $conn = $MYSQL_CONN;
282 return mysql_get_server_info($conn);
286 * Returns a string describing the type of SQL connection in use for the connection or FALSE on failure
288 function sql_get_host_info($conn=false)
291 if (!$conn) $conn = $MYSQL_CONN;
292 return mysql_get_host_info($conn);
296 * Returns the SQL protocol on success, or FALSE on failure.
298 function sql_get_proto_info($conn=false)
301 if (!$conn) $conn = $MYSQL_CONN;
302 return mysql_get_proto_info($conn);
306 * Get the name of the specified field in a result
308 function sql_field_name($res, $offset = 0)
310 return mysql_field_name($res, $offset);
313 /**************************************************************************
314 Unimplemented mysql_* functions
316 # mysql_ data_ seek (maybe useful)
317 # mysql_ errno (maybe useful)
318 # mysql_ fetch_ lengths (maybe useful)
319 # mysql_ field_ flags (maybe useful)
320 # mysql_ field_ len (maybe useful)
321 # mysql_ field_ seek (maybe useful)
322 # mysql_ field_ table (maybe useful)
323 # mysql_ field_ type (maybe useful)
324 # mysql_ info (maybe useful)
325 # mysql_ list_ processes (maybe useful)
326 # mysql_ ping (maybe useful)
327 # mysql_ set_ charset (maybe useful, requires php >=5.2.3 and mysql >=5.0.7)
328 # mysql_ thread_ id (maybe useful)
330 # mysql_ db_ name (useful only if working on multiple dbs which we do not do)
331 # mysql_ list_ dbs (useful only if working on multiple dbs which we do not do)
333 # mysql_ pconnect (probably not useful and could cause some unintended performance issues)
334 # mysql_ unbuffered_ query (possibly useful, but complicated and not supported by all database drivers (pdo))
336 # mysql_ change_ user (deprecated)
337 # mysql_ create_ db (deprecated)
338 # mysql_ db_ query (deprecated)
339 # mysql_ drop_ db (deprecated)
340 # mysql_ escape_ string (deprecated)
341 # mysql_ list_ fields (deprecated)
342 # mysql_ list_ tables (deprecated)
343 # mysql_ tablename (deprecated)
345 *******************************************************************/