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
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) {
35 <html xmlns="http://www.w3.org/1999/xhtml">
36 <head><title><?php echo htmlspecialchars($title)?></title></head>
38 <h1><?php echo htmlspecialchars($title)?></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 $resource = sql_query("show variables LIKE 'character_set_database'");
68 $fetchDat = sql_fetch_assoc($resource);
69 $charset = $fetchDat['Value'];
70 $mySqlVer = implode('.', array_map('intval', explode('.', sql_get_server_info($MYSQL_CONN))));
71 if ($mySqlVer >= '5.0.7' && phpversion() >= '5.2.3') {
72 mysql_set_charset($charset);
73 } elseif ($mySqlVer >= '4.1.0') {
74 sql_query("SET CHARACTER SET " . $charset);
76 // </add for garble measure>*/
82 * disconnects from SQL server
84 function sql_disconnect($conn = false) {
86 if (!$conn) $conn = $MYSQL_CONN;
90 function sql_close($conn = false) {
92 if (!$conn) $conn = $MYSQL_CONN;
97 * executes an SQL query
99 function sql_query($query, $conn = false) {
100 global $SQLCount,$MYSQL_CONN;
101 if (!$conn) $conn = $MYSQL_CONN;
103 $res = mysql_query($query,$conn) or print("mySQL error with query $query: " . mysql_error($conn) . '<p />');
108 * executes an SQL error
110 function sql_error($conn = false)
113 if (!$conn) $conn = $MYSQL_CONN;
114 return mysql_error($conn);
118 * executes an SQL db select
120 function sql_select_db($db,$conn = false)
123 if (!$conn) $conn = $MYSQL_CONN;
124 return mysql_select_db($db,$conn);
128 * executes an SQL real escape
130 function sql_real_escape_string($val,$conn = false)
133 if (!$conn) $conn = $MYSQL_CONN;
134 return mysql_real_escape_string($val,$conn);
138 * executes an SQL insert id
140 function sql_insert_id($conn = false)
143 if (!$conn) $conn = $MYSQL_CONN;
144 return mysql_insert_id($conn);
148 * executes an SQL result request
150 function sql_result($res, $row, $col)
152 return mysql_result($res,$row,$col);
156 * frees sql result resources
158 function sql_free_result($res)
160 return mysql_free_result($res);
164 * returns number of rows in SQL result
166 function sql_num_rows($res)
168 return mysql_num_rows($res);
172 * returns number of rows affected by SQL query
174 function sql_affected_rows($res)
176 return mysql_affected_rows($res);
180 * Get number of fields in result
182 function sql_num_fields($res)
184 return mysql_num_fields($res);
188 * fetches next row of SQL result as an associative array
190 function sql_fetch_assoc($res)
192 return mysql_fetch_assoc($res);
196 * Fetch a result row as an associative array, a numeric array, or both
198 function sql_fetch_array($res)
200 return mysql_fetch_array($res);
204 * fetches next row of SQL result as an object
206 function sql_fetch_object($res)
208 return mysql_fetch_object($res);
212 * Get a result row as an enumerated array
214 function sql_fetch_row($res)
216 return mysql_fetch_row($res);
220 * Get column information from a result and return as an object
222 function sql_fetch_field($res,$offset = 0)
224 return mysql_fetch_field($res,$offset);
228 * Get current system status (returns string)
230 function sql_stat($conn=false)
233 if (!$conn) $conn = $MYSQL_CONN;
234 return mysql_stat($conn);
238 * Returns the name of the character set
240 function sql_client_encoding($conn=false)
243 if (!$conn) $conn = $MYSQL_CONN;
244 return mysql_client_encoding($conn);
248 * Get SQL client version
250 function sql_get_client_info()
252 return mysql_get_client_info();
256 * Get SQL server version
258 function sql_get_server_info($conn=false)
261 if (!$conn) $conn = $MYSQL_CONN;
262 return mysql_get_server_info($conn);
266 * Returns a string describing the type of SQL connection in use for the connection or FALSE on failure
268 function sql_get_host_info($conn=false)
271 if (!$conn) $conn = $MYSQL_CONN;
272 return mysql_get_host_info($conn);
276 * Returns the SQL protocol on success, or FALSE on failure.
278 function sql_get_proto_info($conn=false)
281 if (!$conn) $conn = $MYSQL_CONN;
282 return mysql_get_proto_info($conn);
287 /**************************************************************************
288 Unimplemented mysql_* functions
290 # mysql_ change_ user
297 # mysql_ escape_ string
298 # mysql_ fetch_ lengths
299 # mysql_ field_ flags
303 # mysql_ field_ table
307 # mysql_ list_ fields
308 # mysql_ list_ processes
309 # mysql_ list_ tables
312 # mysql_ set_ charset
315 # mysql_ unbuffered_ query
316 *******************************************************************/