OSDN Git Service

856579f67703969111398da517ae0e627ed05a2e
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / sql / mysql.php
1 <?php
2
3 /*
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5  * Copyright (C) 2002-2011 The Nucleus Group
6  *
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)
12  */
13 /**
14  * @license http://nucleuscms.org/license.txt GNU General Public License
15  * @copyright Copyright (C) 2002-2011 The Nucleus Group
16  * @version $Id$
17  */
18  
19 /*
20  * complete sql_* wrappers for mysql functions
21  *
22  * functions moved from globalfunctions.php: sql_connect, sql_disconnect, sql_query
23  */
24
25
26 $MYSQL_CONN = 0;
27
28 if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
29 {
30         /**
31          *Errors before the database connection has been made
32          */
33         function startUpError($msg, $title) {
34                 if (!defined('_CHARSET')) {
35                         define('_CHARSET', 'UTF-8');
36                 }
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"');
39                 }
40                 sendContentType('text/html','',_CHARSET);
41                 ?>
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>
45         <body>
46                 <h1><?php echo htmlspecialchars($title,ENT_QUOTES)?></h1>
47                 <?php echo $msg?> 
48         </body>
49 </html>
50 <?php
51                 exit;
52         }
53
54         /**
55           * Connects to mysql server with arguments
56           */
57         function sql_connect_args($mysql_host = 'localhost', $mysql_user = '', $mysql_password = '', $mysql_database = '') {
58                 
59                 $CONN = @mysql_connect($mysql_host, $mysql_user, $mysql_password);
60                 if ($mysql_database) mysql_select_db($mysql_database,$CONN);
61
62                 return $CONN;
63         }
64         
65         /**
66           * Connects to mysql server
67           */
68         function sql_connect() {
69                 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN;
70
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');
73 /*
74 // <add for garble measure>
75                 $resource = sql_query("show variables LIKE 'character_set_database'");
76                 $fetchDat = sql_fetch_assoc($resource);
77                 $charset  = $fetchDat['Value'];
78                 $mySqlVer = implode('.', array_map('intval', explode('.', sql_get_server_info($MYSQL_CONN))));
79                 if ($mySqlVer >= '5.0.7' && function_exists('mysql_set_charset')) {
80                         mysql_set_charset($charset);
81                 } elseif ($mySqlVer >= '4.1.0') {
82                         sql_query("SET CHARACTER SET " . $charset);
83                 }
84 // </add for garble measure>
85 */
86                 return $MYSQL_CONN;
87         }
88
89         /**
90           * disconnects from SQL server
91           */
92         function sql_disconnect($conn = false) {
93                 global $MYSQL_CONN;
94                 if (!$conn) $conn = $MYSQL_CONN;
95                 @mysql_close($conn);
96         }
97         
98         function sql_close($conn = false) {
99                 global $MYSQL_CONN;
100                 if (!$conn) $conn = $MYSQL_CONN;
101                 @mysql_close($conn);
102         }
103         
104         /**
105           * executes an SQL query
106           */
107         function sql_query($query, $conn = false) {
108                 global $SQLCount,$MYSQL_CONN;
109                 if (!$conn) $conn = $MYSQL_CONN;
110                 $SQLCount++;
111                 $res = mysql_query($query,$conn) or print("mySQL error with query $query: " . mysql_error($conn) . '<p />');
112                 return $res;
113         }
114         
115         /**
116           * executes an SQL error
117           */
118         function sql_error($conn = false)
119         {
120                 global $MYSQL_CONN;
121                 if (!$conn) $conn = $MYSQL_CONN;
122                 return mysql_error($conn);
123         }
124         
125         /**
126           * executes an SQL db select
127           */
128         function sql_select_db($db,$conn = false)
129         {
130                 global $MYSQL_CONN;
131                 if (!$conn) $conn = $MYSQL_CONN;
132                 return mysql_select_db($db,$conn);
133         }
134         
135         /**
136           * executes an SQL real escape 
137           */
138         function sql_real_escape_string($val,$conn = false)
139         {
140                 global $MYSQL_CONN;
141                 if (!$conn) $conn = $MYSQL_CONN;
142                 return mysql_real_escape_string($val,$conn);
143         }
144         
145         /**
146           * executes an PDO::quote() like escape, ie adds quotes arround the string and escapes chars as needed 
147           */
148         function sql_quote_string($val,$conn = false) {
149                 global $MYSQL_CONN;
150                 if (!$conn) $conn = $MYSQL_CONN;
151                 return "'".mysql_real_escape_string($val,$conn)."'";
152         }
153         
154         /**
155           * executes an SQL insert id
156           */
157         function sql_insert_id($conn = false)
158         {
159                 global $MYSQL_CONN;
160                 if (!$conn) $conn = $MYSQL_CONN;
161                 return mysql_insert_id($conn);
162         }
163         
164         /**
165           * executes an SQL result request
166           */
167         function sql_result($res, $row, $col)
168         {
169                 return mysql_result($res,$row,$col);
170         }
171         
172         /**
173           * frees sql result resources
174           */
175         function sql_free_result($res)
176         {
177                 return mysql_free_result($res);
178         }
179         
180         /**
181          * returns number of rows in SQL result
182          */
183         function sql_num_rows($res)
184         {
185                 return mysql_num_rows($res);
186         }
187         
188         /**
189          * returns number of rows affected by SQL query
190          */
191         function sql_affected_rows($conn = false)
192         {
193                 global $MYSQL_CONN;
194                 if (!$conn) $conn = $MYSQL_CONN;
195                 return mysql_affected_rows($conn);
196         }
197         
198         /**
199           * Get number of fields in result
200           */
201         function sql_num_fields($res)
202         {
203                 return mysql_num_fields($res);
204         }
205         
206         /**
207           * fetches next row of SQL result as an associative array
208           */
209         function sql_fetch_assoc($res)
210         {
211                 return mysql_fetch_assoc($res);
212         }
213         
214         /**
215           * Fetch a result row as an associative array, a numeric array, or both
216           */
217         function sql_fetch_array($res)
218         {
219                 return mysql_fetch_array($res);
220         }
221         
222         /**
223           * fetches next row of SQL result as an object
224           */
225         function sql_fetch_object($res)
226         {
227                 return mysql_fetch_object($res);
228         }
229         
230         /**
231           * Get a result row as an enumerated array
232           */
233         function sql_fetch_row($res)
234         {
235                 return mysql_fetch_row($res);
236         }
237         
238         /**
239           * Get column information from a result and return as an object
240           */
241         function sql_fetch_field($res,$offset = 0)
242         {
243                 return mysql_fetch_field($res,$offset);
244         }
245         
246         /**
247           * Get current system status (returns string)
248           */
249         function sql_stat($conn=false)
250         {
251                 global $MYSQL_CONN;
252                 if (!$conn) $conn = $MYSQL_CONN;
253                 return mysql_stat($conn);
254         }
255         
256         /**
257           * Returns the name of the character set
258           */
259         function sql_client_encoding($conn=false)
260         {
261                 global $MYSQL_CONN;
262                 if (!$conn) $conn = $MYSQL_CONN;
263                 return mysql_client_encoding($conn);
264         }
265         
266         /**
267           * Get SQL client version
268           */
269         function sql_get_client_info()
270         {
271                 return mysql_get_client_info();
272         }
273         
274         /**
275           * Get SQL server version
276           */
277         function sql_get_server_info($conn=false)
278         {
279                 global $MYSQL_CONN;
280                 if (!$conn) $conn = $MYSQL_CONN;
281                 return mysql_get_server_info($conn);
282         }
283         
284         /**
285           * Returns a string describing the type of SQL connection in use for the connection or FALSE on failure
286           */
287         function sql_get_host_info($conn=false)
288         {
289                 global $MYSQL_CONN;
290                 if (!$conn) $conn = $MYSQL_CONN;
291                 return mysql_get_host_info($conn);
292         }
293         
294         /**
295           * Returns the SQL protocol on success, or FALSE on failure. 
296           */
297         function sql_get_proto_info($conn=false)
298         {
299                 global $MYSQL_CONN;
300                 if (!$conn) $conn = $MYSQL_CONN;
301                 return mysql_get_proto_info($conn);
302         }
303
304         /**
305          * Get the name of the specified field in a result
306          */
307         function sql_field_name($res, $offset = 0)
308         {
309                 return mysql_field_name($res, $offset);
310         }
311
312 /**************************************************************************
313         Unimplemented mysql_* functions
314         
315 # mysql_ data_ seek (maybe useful)
316 # mysql_ errno (maybe useful)
317 # mysql_ fetch_ lengths (maybe useful)
318 # mysql_ field_ flags (maybe useful)
319 # mysql_ field_ len (maybe useful)
320 # mysql_ field_ seek (maybe useful)
321 # mysql_ field_ table (maybe useful)
322 # mysql_ field_ type (maybe useful)
323 # mysql_ info (maybe useful)
324 # mysql_ list_ processes (maybe useful)
325 # mysql_ ping (maybe useful)
326 # mysql_ set_ charset (maybe useful, requires php >=5.2.3 and mysql >=5.0.7)
327 # mysql_ thread_ id (maybe useful)
328
329 # mysql_ db_ name (useful only if working on multiple dbs which we do not do)
330 # mysql_ list_ dbs (useful only if working on multiple dbs which we do not do)
331
332 # mysql_ pconnect (probably not useful and could cause some unintended performance issues)
333 # mysql_ unbuffered_ query (possibly useful, but complicated and not supported by all database drivers (pdo))
334
335 # mysql_ change_ user (deprecated)
336 # mysql_ create_ db (deprecated)
337 # mysql_ db_ query (deprecated)
338 # mysql_ drop_ db (deprecated)
339 # mysql_ escape_ string (deprecated)
340 # mysql_ list_ fields (deprecated)
341 # mysql_ list_ tables (deprecated)
342 # mysql_ tablename (deprecated)
343
344 *******************************************************************/
345
346         /**
347           * for JP installer only
348           */
349         function at_sql_query($query, $conn = false) {
350                 global $SQLCount,$MYSQL_CONN;
351                 if (!$conn) $conn = $MYSQL_CONN;
352                 $SQLCount++;
353                 $res = mysql_query($query,$conn);
354                 return $res;
355         }
356 }
357 ?>