4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
5 * Copyright (C) 2002-2009 The Nucleus Group
\r
7 * This program is free software; you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation; either version 2
\r
10 * of the License, or (at your option) any later version.
\r
11 * (see nucleus/documentation/index.html#license for more info)
\r
14 * @license http://nucleuscms.org/license.txt GNU General Public License
\r
15 * @copyright Copyright (C) 2002-2009 The Nucleus Group
\r
20 * complete sql_* wrappers for mysql functions
\r
22 * functions moved from globalfunctions.php: sql_connect, sql_disconnect, sql_query
\r
28 if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
\r
31 * Errors before the database connection has been made
\r
33 function startUpError($msg, $title)
\r
36 <html xmlns="http://www.w3.org/1999/xhtml">
\r
37 <head><title><?php echo Entity::hsc($title)?></title></head>
\r
39 <h1><?php echo Entity::hsc($title)?></h1>
\r
48 * Connects to mysql server with arguments
\r
50 function sql_connect_args($mysql_host = 'localhost', $mysql_user = '', $mysql_password = '', $mysql_database = '', $new_link = FALSE)
\r
52 $CONN = @mysql_connect($mysql_host, $mysql_user, $mysql_password, $new_link);
\r
54 if ( $mysql_database )
\r
56 mysql_select_db($mysql_database,$CONN);
\r
57 sql_set_charset('utf8');
\r
62 $result = sql_query('SHOW VARIABLES LIKE \'char%\';');
\r
63 while(FALSE !== ($row = sql_fetch_row($result)))
\r
65 echo "{$row[0]}: {$row[1]}\n";
\r
74 * Connects to mysql server
\r
76 function sql_connect()
\r
78 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN;
\r
80 $MYSQL_CONN = @mysql_connect($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD) or startUpError('<p>Could not connect to MySQL database.</p>', 'Connect Error');
\r
81 mysql_select_db($MYSQL_DATABASE) or startUpError('<p>Could not select database: ' . mysql_error() . '</p>', 'Connect Error');
\r
83 sql_set_charset('utf8');
\r
87 $result = sql_query('SHOW VARIABLES LIKE \'char%\';');
\r
88 while(FALSE !== ($row = sql_fetch_row($result)))
\r
90 echo "{$row[0]}: {$row[1]}\n";
\r
99 * disconnects from SQL server
\r
101 function sql_disconnect($conn = false) {
\r
102 global $MYSQL_CONN;
\r
103 if (!$conn) $conn = $MYSQL_CONN;
\r
104 @mysql_close($conn);
\r
107 function sql_close($conn = false) {
\r
108 global $MYSQL_CONN;
\r
109 if (!$conn) $conn = $MYSQL_CONN;
\r
110 @mysql_close($conn);
\r
114 * executes an SQL query
\r
116 function sql_query($query,$conn = false) {
\r
117 global $SQLCount,$MYSQL_CONN;
\r
118 if (!$conn) $conn = $MYSQL_CONN;
\r
120 $res = mysql_query($query,$conn) or print("mySQL error with query $query: " . mysql_error($conn) . '<p />');
\r
125 * executes an SQL error
\r
127 function sql_error($conn = false)
\r
129 global $MYSQL_CONN;
\r
130 if (!$conn) $conn = $MYSQL_CONN;
\r
131 return mysql_error($conn);
\r
135 * executes an SQL db select
\r
137 function sql_select_db($db,$conn = false)
\r
139 global $MYSQL_CONN;
\r
140 if (!$conn) $conn = $MYSQL_CONN;
\r
141 return mysql_select_db($db,$conn);
\r
145 * executes an SQL real escape
\r
147 function sql_real_escape_string($val, $conn = false)
\r
149 global $MYSQL_CONN;
\r
152 $conn =& $MYSQL_CONN;
\r
154 return mysql_real_escape_string($val, $conn);
\r
158 * executes an PDO::quote() like escape, ie adds quotes arround the string and escapes chars as needed
\r
160 function sql_quote_string($val,$conn = false) {
\r
161 global $MYSQL_CONN;
\r
162 if (!$conn) $conn = $MYSQL_CONN;
\r
163 return "'".mysql_real_escape_string($val,$conn)."'";
\r
167 * executes an SQL insert id
\r
169 function sql_insert_id($conn = false)
\r
171 global $MYSQL_CONN;
\r
172 if (!$conn) $conn = $MYSQL_CONN;
\r
173 return mysql_insert_id($conn);
\r
177 * executes an SQL result request
\r
179 function sql_result($res, $row, $col)
\r
181 return mysql_result($res,$row,$col);
\r
185 * frees sql result resources
\r
187 function sql_free_result($res)
\r
189 return mysql_free_result($res);
\r
193 * returns number of rows in SQL result
\r
195 function sql_num_rows($res)
\r
197 return mysql_num_rows($res);
\r
201 * returns number of rows affected by SQL query
\r
203 function sql_affected_rows($conn = false)
\r
205 global $MYSQL_CONN;
\r
206 if (!$conn) $conn = $MYSQL_CONN;
\r
207 return mysql_affected_rows($conn);
\r
211 * Get number of fields in result
\r
213 function sql_num_fields($res)
\r
215 return mysql_num_fields($res);
\r
219 * fetches next row of SQL result as an associative array
\r
221 function sql_fetch_assoc($res)
\r
223 return mysql_fetch_assoc($res);
\r
227 * Fetch a result row as an associative array, a numeric array, or both
\r
229 function sql_fetch_array($res)
\r
231 return mysql_fetch_array($res);
\r
235 * fetches next row of SQL result as an object
\r
237 function sql_fetch_object($res)
\r
239 return mysql_fetch_object($res);
\r
243 * Get a result row as an enumerated array
\r
245 function sql_fetch_row($res)
\r
247 return mysql_fetch_row($res);
\r
251 * Get column information from a result and return as an object
\r
253 function sql_fetch_field($res,$offset = 0)
\r
255 return mysql_fetch_field($res,$offset);
\r
259 * Get current system status (returns string)
\r
261 function sql_stat($conn=false)
\r
263 global $MYSQL_CONN;
\r
264 if (!$conn) $conn = $MYSQL_CONN;
\r
265 return mysql_stat($conn);
\r
269 * Returns the name of the character set
\r
271 function sql_client_encoding($conn=false)
\r
273 global $MYSQL_CONN;
\r
274 if (!$conn) $conn = $MYSQL_CONN;
\r
275 return mysql_client_encoding($conn);
\r
279 * Get SQL client version
\r
281 function sql_get_client_info()
\r
283 return mysql_get_client_info();
\r
287 * Get SQL server version
\r
289 function sql_get_server_info($conn=false)
\r
291 global $MYSQL_CONN;
\r
292 if (!$conn) $conn = $MYSQL_CONN;
\r
293 return mysql_get_server_info($conn);
\r
297 * Returns a string describing the type of SQL connection in use for the connection or FALSE on failure
\r
299 function sql_get_host_info($conn=false)
\r
301 global $MYSQL_CONN;
\r
302 if (!$conn) $conn = $MYSQL_CONN;
\r
303 return mysql_get_host_info($conn);
\r
307 * Returns the SQL protocol on success, or FALSE on failure.
\r
309 function sql_get_proto_info($conn=false)
\r
311 global $MYSQL_CONN;
\r
312 if (!$conn) $conn = $MYSQL_CONN;
\r
313 return mysql_get_proto_info($conn);
\r
317 * Get the name of the specified field in a result
\r
319 function sql_field_name($res, $offset = 0)
\r
321 return mysql_field_name($res, $offset);
\r
325 * Set character encodings in each fields related to MySQL connection.
\r
327 function sql_set_charset($charset)
\r
329 global $MYSQL_CONN;
\r
334 * We decided to ignore which character encodings is set in each text field of MySQL table!
\r
336 * There are differences between "SET NAMES xxx;" and "SET CHARACTER SET xxx;"
\r
337 * according MySQL version.
\r
338 * http://dev.mysql.com/doc/refman/4.1/en/charset-connection.html
\r
339 * http://dev.mysql.com/doc/refman/5.1/en/charset-connection.html
\r
341 * And mysql_set_charset() execute "SET NAMES xxx;" internally and set mysql->charset as xxx.
\r
342 * refering to MySQL C API.
\r
343 * http://dev.mysql.com/doc/refman/5.1/ja/mysql-set-character-set.html
\r
344 * http://php.net/manual/en/function.mysql-set-charset.php
\r
346 * mysql_real_escape_string() is affected by mysql->charset,
\r
347 * refering to MySQL C API.
\r
348 * http://dev.mysql.com/doc/refman/5.1/en/mysql-real-escape-string.html
\r
349 * http://php.net/manual/en/function.mysql-real-escape-string.php
\r
351 * But using the same character encoding in character_set_client and the strings is
\r
352 * more important than mysql->charset
\r
353 * because mysql_real_escape_string() escape some characters in ASCII character set.
\r
356 $charset = strtolower($charset);
\r
357 $mysql_version = preg_replace('#^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{1,2})(.*)$#', '$1.$2.$3', sql_get_server_info($MYSQL_CONN));
\r
359 if ( version_compare($mysql_version, '5.0.7', '>=') && function_exists('mysql_set_charset') )
\r
361 $result = mysql_set_charset($charset);
\r
363 else if ( version_compare($mysql_version, '5.0.7', '<') )
\r
365 $result = sql_query("SET CHARACTER SET {$charset};");
\r
369 $result = sql_query("SET NAMES {$charset};");
\r
375 /**************************************************************************
\r
376 Unimplemented mysql_* functions
\r
378 # mysql_ data_ seek (maybe useful)
\r
379 # mysql_ errno (maybe useful)
\r
380 # mysql_ fetch_ lengths (maybe useful)
\r
381 # mysql_ field_ flags (maybe useful)
\r
382 # mysql_ field_ len (maybe useful)
\r
383 # mysql_ field_ seek (maybe useful)
\r
384 # mysql_ field_ table (maybe useful)
\r
385 # mysql_ field_ type (maybe useful)
\r
386 # mysql_ info (maybe useful)
\r
387 # mysql_ list_ processes (maybe useful)
\r
388 # mysql_ ping (maybe useful)
\r
389 # mysql_ set_ charset (maybe useful, requires php >=5.2.3 and mysql >=5.0.7)
\r
390 # mysql_ thread_ id (maybe useful)
\r
392 # mysql_ db_ name (useful only if working on multiple dbs which we do not do)
\r
393 # mysql_ list_ dbs (useful only if working on multiple dbs which we do not do)
\r
395 # mysql_ pconnect (probably not useful and could cause some unintended performance issues)
\r
396 # mysql_ unbuffered_ query (possibly useful, but complicated and not supported by all database drivers (pdo))
\r
398 # mysql_ change_ user (deprecated)
\r
399 # mysql_ create_ db (deprecated)
\r
400 # mysql_ db_ query (deprecated)
\r
401 # mysql_ drop_ db (deprecated)
\r
402 # mysql_ escape_ string (deprecated)
\r
403 # mysql_ list_ fields (deprecated)
\r
404 # mysql_ list_ tables (deprecated)
\r
405 # mysql_ tablename (deprecated)
\r
407 *******************************************************************/
\r