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
20 if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
23 *Errors before the database connection has been made
25 function startUpError($msg, $title) {
26 if (!defined('_CHARSET')) {
27 define('_CHARSET', 'UTF-8');
29 if (!defined('_HTML_XML_NAME_SPACE_AND_LANG_CODE')) {
30 define('_HTML_XML_NAME_SPACE_AND_LANG_CODE', 'xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us"');
32 sendContentType('text/html','',_CHARSET);
34 <html <?php echo _HTML_XML_NAME_SPACE_AND_LANG_CODE; ?>>
35 <head><meta http-equiv="Content-Type" content="text/html; charset=<?php echo _CHARSET?>" />
36 <title><?php echo htmlspecialchars($title,ENT_QUOTES)?></title></head>
38 <h1><?php echo htmlspecialchars($title,ENT_QUOTES)?></h1>
47 * Connects to mysql server with arguments
49 function sql_connect_args($mysql_host = 'localhost', $mysql_user = '', $mysql_password = '', $mysql_database = '') {
51 $CONN = @mysql_connect($mysql_host, $mysql_user, $mysql_password);
52 if ($mysql_database) mysql_select_db($mysql_database,$CONN);
58 * Connects to mysql server
60 function sql_connect() {
61 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN;
63 $MYSQL_CONN = @mysql_connect($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD) or startUpError('<p>Could not connect to MySQL database.</p>', 'Connect Error');
64 mysql_select_db($MYSQL_DATABASE) or startUpError('<p>Could not select database: ' . mysql_error() . '</p>', 'Connect Error');
66 // <add for garble measure>
67 if (defined('_CHARSET')){
70 $resource = sql_query("show variables LIKE 'character_set_database'");
71 $fetchDat = sql_fetch_assoc($resource);
72 $charset = $fetchDat['Value'];
73 // in trouble of encoding,uncomment the following line.
77 sql_set_charset_jp($charset);
78 // </add for garble measure>*/
84 * disconnects from SQL server
86 function sql_disconnect($conn = false) {
88 if (!$conn) $conn = $MYSQL_CONN;
92 function sql_close($conn = false) {
94 if (!$conn) $conn = $MYSQL_CONN;
99 * executes an SQL query
101 function sql_query($query, $conn = false) {
102 global $SQLCount,$MYSQL_CONN;
103 if (!$conn) $conn = $MYSQL_CONN;
105 $res = mysql_query($query,$conn) or print("mySQL error with query $query: " . mysql_error($conn) . '<p />');
110 * executes an SQL error
112 function sql_error($conn = false)
115 if (!$conn) $conn = $MYSQL_CONN;
116 return mysql_error($conn);
120 * executes an SQL db select
122 function sql_select_db($db,$conn = false)
125 if (!$conn) $conn = $MYSQL_CONN;
126 return mysql_select_db($db,$conn);
130 * executes an SQL real escape
132 function sql_real_escape_string($val,$conn = false)
135 if (!$conn) $conn = $MYSQL_CONN;
136 return mysql_real_escape_string($val,$conn);
140 * executes an PDO::quote() like escape, ie adds quotes arround the string and escapes chars as needed
142 function sql_quote_string($val,$conn = false) {
144 if (!$conn) $conn = $MYSQL_CONN;
145 return "'".mysql_real_escape_string($val,$conn)."'";
149 * executes an SQL insert id
151 function sql_insert_id($conn = false)
154 if (!$conn) $conn = $MYSQL_CONN;
155 return mysql_insert_id($conn);
159 * executes an SQL result request
161 function sql_result($res, $row, $col)
163 return mysql_result($res,$row,$col);
167 * frees sql result resources
169 function sql_free_result($res)
171 return mysql_free_result($res);
175 * returns number of rows in SQL result
177 function sql_num_rows($res)
179 return mysql_num_rows($res);
183 * returns number of rows affected by SQL query
185 function sql_affected_rows($conn = false)
188 if (!$conn) $conn = $MYSQL_CONN;
189 return mysql_affected_rows($conn);
193 * Get number of fields in result
195 function sql_num_fields($res)
197 return mysql_num_fields($res);
201 * fetches next row of SQL result as an associative array
203 function sql_fetch_assoc($res)
205 return mysql_fetch_assoc($res);
209 * Fetch a result row as an associative array, a numeric array, or both
211 function sql_fetch_array($res)
213 return mysql_fetch_array($res);
217 * fetches next row of SQL result as an object
219 function sql_fetch_object($res)
221 return mysql_fetch_object($res);
225 * Get a result row as an enumerated array
227 function sql_fetch_row($res)
229 return mysql_fetch_row($res);
233 * Get column information from a result and return as an object
235 function sql_fetch_field($res,$offset = 0)
237 return mysql_fetch_field($res,$offset);
241 * Get current system status (returns string)
243 function sql_stat($conn=false)
246 if (!$conn) $conn = $MYSQL_CONN;
247 return mysql_stat($conn);
251 * Returns the name of the character set
253 function sql_client_encoding($conn=false)
256 if (!$conn) $conn = $MYSQL_CONN;
257 return mysql_client_encoding($conn);
261 * Get SQL client version
263 function sql_get_client_info()
265 return mysql_get_client_info();
269 * Get SQL server version
271 function sql_get_server_info($conn=false)
274 if (!$conn) $conn = $MYSQL_CONN;
275 return mysql_get_server_info($conn);
279 * Returns a string describing the type of SQL connection in use for the connection or FALSE on failure
281 function sql_get_host_info($conn=false)
284 if (!$conn) $conn = $MYSQL_CONN;
285 return mysql_get_host_info($conn);
289 * Returns the SQL protocol on success, or FALSE on failure.
291 function sql_get_proto_info($conn=false)
294 if (!$conn) $conn = $MYSQL_CONN;
295 return mysql_get_proto_info($conn);
299 * Get the name of the specified field in a result
301 function sql_field_name($res, $offset = 0)
303 return mysql_field_name($res, $offset);
306 /**************************************************************************
307 Unimplemented mysql_* functions
309 # mysql_ data_ seek (maybe useful)
310 # mysql_ errno (maybe useful)
311 # mysql_ fetch_ lengths (maybe useful)
312 # mysql_ field_ flags (maybe useful)
313 # mysql_ field_ len (maybe useful)
314 # mysql_ field_ seek (maybe useful)
315 # mysql_ field_ table (maybe useful)
316 # mysql_ field_ type (maybe useful)
317 # mysql_ info (maybe useful)
318 # mysql_ list_ processes (maybe useful)
319 # mysql_ ping (maybe useful)
320 # mysql_ set_ charset (maybe useful, requires php >=5.2.3 and mysql >=5.0.7)
321 # mysql_ thread_ id (maybe useful)
323 # mysql_ db_ name (useful only if working on multiple dbs which we do not do)
324 # mysql_ list_ dbs (useful only if working on multiple dbs which we do not do)
326 # mysql_ pconnect (probably not useful and could cause some unintended performance issues)
327 # mysql_ unbuffered_ query (possibly useful, but complicated and not supported by all database drivers (pdo))
329 # mysql_ change_ user (deprecated)
330 # mysql_ create_ db (deprecated)
331 # mysql_ db_ query (deprecated)
332 # mysql_ drop_ db (deprecated)
333 # mysql_ escape_ string (deprecated)
334 # mysql_ list_ fields (deprecated)
335 # mysql_ list_ tables (deprecated)
336 # mysql_ tablename (deprecated)
338 *******************************************************************/
341 * for preventing I/O strings from auto-detecting the charactor encodings by MySQL
343 * Jan.20, 2011 by kotorisan and cacher
344 * refering to their conversation below,
345 * http://japan.nucleuscms.org/bb/viewtopic.php?p=26581
347 * NOTE: shift_jis is only supported for output. Using shift_jis in DB is prohibited.
348 * NOTE: iso-8859-x,windows-125x if _CHARSET is unset.
350 function sql_set_charset_jp($charset) {
351 switch(strtolower($charset)){
\r
361 $charset = 'gb2312';
\r
370 $charset = 'latin1';
\r
373 $mySqlVer = implode('.', array_map('intval', explode('.', sql_get_server_info())));
374 if (version_compare($mySqlVer, '5.0.7', '>=') && function_exists('mysql_set_charset')) {
375 $res = mysql_set_charset($charset);
376 } elseif (version_compare($mySqlVer, '4.1.0', '>=')) {
377 $res = sql_query("SET CHARACTER SET " . $charset);