OSDN Git Service

本家Nucleus CMSの開発を補助するためにコミット
[nucleus-jp/nucleus-next.git] / nucleus / libs / sql / mysql.php
1 <?php
2
3 /*
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5  * Copyright (C) 2002-2009 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-2009 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                 ?>
35                 <html xmlns="http://www.w3.org/1999/xhtml">
36                         <head><title><?php echo i18n::hsc($title)?></title></head>
37                         <body>
38                                 <h1><?php echo i18n::hsc($title)?></h1>
39                                 <?php echo $msg?>
40                         </body>
41                 </html>
42 <?php
43                 exit;
44         }
45         
46         /**
47           * Connects to mysql server with arguments
48           */
49         function sql_connect_args($mysql_host = 'localhost', $mysql_user = '', $mysql_password = '', $mysql_database = '') {
50                 
51                 $CONN = @mysql_connect($mysql_host, $mysql_user, $mysql_password); 
52                 
53                 if ( $mysql_database )
54                 {
55                         mysql_select_db($mysql_database,$CONN);
56                         sql_set_charset('utf8');
57                 }
58                 
59                 /*
60                 // For debugging
61                 $result = sql_query('SHOW VARIABLES LIKE \'char%\';');
62                 while(FALSE !== ($row = sql_fetch_row($result)))
63                 {
64                         echo "{$row[0]}: {$row[1]}\n";
65                 }
66                 */
67                 
68                 return $CONN;
69         }
70         
71         /**
72           * Connects to mysql server
73           */
74         function sql_connect() {
75                 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN;
76
77                 $MYSQL_CONN = @mysql_connect($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD) or startUpError('<p>Could not connect to MySQL database.</p>', 'Connect Error');
78                 mysql_select_db($MYSQL_DATABASE) or startUpError('<p>Could not select database: ' . mysql_error() . '</p>', 'Connect Error');
79                 
80                 sql_set_charset('utf8');
81                 
82                 /*
83                 // For debugging
84                 $result = sql_query('SHOW VARIABLES LIKE \'char%\';');
85                 while(FALSE !== ($row = sql_fetch_row($result)))
86                 {
87                         echo "{$row[0]}: {$row[1]}\n";
88                 }
89                 */
90                 
91                 return $MYSQL_CONN;
92         }
93
94         /**
95           * disconnects from SQL server
96           */
97         function sql_disconnect($conn = false) {
98                 global $MYSQL_CONN;
99                 if (!$conn) $conn = $MYSQL_CONN;
100                 @mysql_close($conn);
101         }
102         
103         function sql_close($conn = false) {
104                 global $MYSQL_CONN;
105                 if (!$conn) $conn = $MYSQL_CONN;
106                 @mysql_close($conn);
107         }
108         
109         /**
110           * executes an SQL query
111           */
112         function sql_query($query,$conn = false) {
113                 global $SQLCount,$MYSQL_CONN;
114                 if (!$conn) $conn = $MYSQL_CONN;
115                 $SQLCount++;
116                 $res = mysql_query($query,$conn) or print("mySQL error with query $query: " . mysql_error($conn) . '<p />');
117                 return $res;
118         }
119         
120         /**
121           * executes an SQL error
122           */
123         function sql_error($conn = false)
124         {
125                 global $MYSQL_CONN;
126                 if (!$conn) $conn = $MYSQL_CONN;
127                 return mysql_error($conn);
128         }
129         
130         /**
131           * executes an SQL db select
132           */
133         function sql_select_db($db,$conn = false)
134         {
135                 global $MYSQL_CONN;
136                 if (!$conn) $conn = $MYSQL_CONN;
137                 return mysql_select_db($db,$conn);
138         }
139         
140         /*
141          * executes an SQL real escape 
142          */
143         function sql_real_escape_string($val, $conn = false)
144         {
145                 global $MYSQL_CONN;
146                 if (!$conn)
147                 {
148                         $conn =& $MYSQL_CONN;
149                 }
150                 return mysql_real_escape_string($val, $conn);
151         }
152         
153         /**
154           * executes an PDO::quote() like escape, ie adds quotes arround the string and escapes chars as needed 
155           */
156         function sql_quote_string($val,$conn = false) {
157                 global $MYSQL_CONN;
158                 if (!$conn) $conn = $MYSQL_CONN;
159                 return "'".mysql_real_escape_string($val,$conn)."'";
160         }
161         
162         /**
163           * executes an SQL insert id
164           */
165         function sql_insert_id($conn = false)
166         {
167                 global $MYSQL_CONN;
168                 if (!$conn) $conn = $MYSQL_CONN;
169                 return mysql_insert_id($conn);
170         }
171         
172         /**
173           * executes an SQL result request
174           */
175         function sql_result($res, $row, $col)
176         {
177                 return mysql_result($res,$row,$col);
178         }
179         
180         /**
181           * frees sql result resources
182           */
183         function sql_free_result($res)
184         {
185                 return mysql_free_result($res);
186         }
187         
188         /**
189           * returns number of rows in SQL result
190           */
191         function sql_num_rows($res)
192         {
193                 return mysql_num_rows($res);
194         }
195         
196         /**
197           * returns number of rows affected by SQL query
198           */
199         function sql_affected_rows($conn = false)
200         {
201                 global $MYSQL_CONN;
202                 if (!$conn) $conn = $MYSQL_CONN;
203                 return mysql_affected_rows($conn);
204         }
205         
206         /**
207           * Get number of fields in result
208           */
209         function sql_num_fields($res)
210         {
211                 return mysql_num_fields($res);
212         }
213         
214         /**
215           * fetches next row of SQL result as an associative array
216           */
217         function sql_fetch_assoc($res)
218         {
219                 return mysql_fetch_assoc($res);
220         }
221         
222         /**
223           * Fetch a result row as an associative array, a numeric array, or both
224           */
225         function sql_fetch_array($res)
226         {
227                 return mysql_fetch_array($res);
228         }
229         
230         /**
231           * fetches next row of SQL result as an object
232           */
233         function sql_fetch_object($res)
234         {
235                 return mysql_fetch_object($res);
236         }
237         
238         /**
239           * Get a result row as an enumerated array
240           */
241         function sql_fetch_row($res)
242         {
243                 return mysql_fetch_row($res);
244         }
245         
246         /**
247           * Get column information from a result and return as an object
248           */
249         function sql_fetch_field($res,$offset = 0)
250         {
251                 return mysql_fetch_field($res,$offset);
252         }
253         
254         /**
255           * Get current system status (returns string)
256           */
257         function sql_stat($conn=false)
258         {
259                 global $MYSQL_CONN;
260                 if (!$conn) $conn = $MYSQL_CONN;
261                 return mysql_stat($conn);
262         }
263         
264         /**
265           * Returns the name of the character set
266           */
267         function sql_client_encoding($conn=false)
268         {
269                 global $MYSQL_CONN;
270                 if (!$conn) $conn = $MYSQL_CONN;
271                 return mysql_client_encoding($conn);
272         }
273         
274         /**
275           * Get SQL client version
276           */
277         function sql_get_client_info()
278         {
279                 return mysql_get_client_info();
280         }
281         
282         /**
283           * Get SQL server version
284           */
285         function sql_get_server_info($conn=false)
286         {
287                 global $MYSQL_CONN;
288                 if (!$conn) $conn = $MYSQL_CONN;
289                 return mysql_get_server_info($conn);
290         }
291         
292         /**
293           * Returns a string describing the type of SQL connection in use for the connection or FALSE on failure
294           */
295         function sql_get_host_info($conn=false)
296         {
297                 global $MYSQL_CONN;
298                 if (!$conn) $conn = $MYSQL_CONN;
299                 return mysql_get_host_info($conn);
300         }
301         
302         /**
303           * Returns the SQL protocol on success, or FALSE on failure. 
304           */
305         function sql_get_proto_info($conn=false)
306         {
307                 global $MYSQL_CONN;
308                 if (!$conn) $conn = $MYSQL_CONN;
309                 return mysql_get_proto_info($conn);
310         }
311
312         /**
313          * Get the name of the specified field in a result
314          */
315         function sql_field_name($res, $offset = 0)
316         {
317                 return mysql_field_name($res, $offset);
318         }
319         
320         /**
321          * Set character encodings in each fields related to MySQL connection.
322          */
323         function sql_set_charset($charset)
324         {
325                 global $MYSQL_CONN;
326                 
327                 /*
328                  * NOTE:
329                  * 
330                  * We decided to ignore which character encodings is set in each text field of MySQL table!
331                  * 
332                  * There are differences between "SET NAMES xxx;" and "SET CHARACTER SET xxx;"
333                  *  according MySQL version.
334                  * http://dev.mysql.com/doc/refman/4.1/en/charset-connection.html
335                  * http://dev.mysql.com/doc/refman/5.1/en/charset-connection.html
336                  * 
337                  * And mysql_set_charset() execute "SET NAMES xxx;" internally and set mysql->charset as xxx.
338                  *  refering to  MySQL C API.
339                  * http://dev.mysql.com/doc/refman/5.1/ja/mysql-set-character-set.html
340                  * http://php.net/manual/en/function.mysql-set-charset.php
341                  * 
342                  * mysql_real_escape_string() is affected by mysql->charset,
343                  *  refering to  MySQL C API.
344                  * http://dev.mysql.com/doc/refman/5.1/en/mysql-real-escape-string.html
345                  * http://php.net/manual/en/function.mysql-real-escape-string.php
346                  * 
347                  * But using the same character encoding in character_set_client and the strings is
348                  *  more important than mysql->charset
349                  *  because mysql_real_escape_string() escape some characters in ASCII character set.
350                  * 
351                  */
352                 $charset = strtolower($charset);
353                 $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));
354                 
355                 if ( version_compare($mysql_version, '5.0.7', '>=') && function_exists('mysql_set_charset') )
356                 {
357                         $result = mysql_set_charset($charset);
358                 }
359                 else if ( version_compare($mysql_version, '5.0.7', '<') )
360                 {
361                         $result = sql_query("SET CHARACTER SET {$charset};");
362                 }
363                 else
364                 {
365                         $result = sql_query("SET NAMES {$charset};");
366                 }
367                 
368                 return $result;
369         }
370         
371 /**************************************************************************
372 Unimplemented mysql_* functions
373
374 # mysql_ data_ seek (maybe useful)
375 # mysql_ errno (maybe useful)
376 # mysql_ fetch_ lengths (maybe useful)
377 # mysql_ field_ flags (maybe useful)
378 # mysql_ field_ len (maybe useful)
379 # mysql_ field_ seek (maybe useful)
380 # mysql_ field_ table (maybe useful)
381 # mysql_ field_ type (maybe useful)
382 # mysql_ info (maybe useful)
383 # mysql_ list_ processes (maybe useful)
384 # mysql_ ping (maybe useful)
385 # mysql_ set_ charset (maybe useful, requires php >=5.2.3 and mysql >=5.0.7)
386 # mysql_ thread_ id (maybe useful)
387
388 # mysql_ db_ name (useful only if working on multiple dbs which we do not do)
389 # mysql_ list_ dbs (useful only if working on multiple dbs which we do not do)
390
391 # mysql_ pconnect (probably not useful and could cause some unintended performance issues)
392 # mysql_ unbuffered_ query (possibly useful, but complicated and not supported by all database drivers (pdo))
393
394 # mysql_ change_ user (deprecated)
395 # mysql_ create_ db (deprecated)
396 # mysql_ db_ query (deprecated)
397 # mysql_ drop_ db (deprecated)
398 # mysql_ escape_ string (deprecated)
399 # mysql_ list_ fields (deprecated)
400 # mysql_ list_ tables (deprecated)
401 # mysql_ tablename (deprecated)
402
403 *******************************************************************/
404
405 }