OSDN Git Service

Subversion由来のタグを削除
[nucleus-jp/nucleus-jp-ancient.git] / nucleus / libs / sql / mysql.php
1 <?php
2
3 /*
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5  * Copyright (C) 2002-2012 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  * complete sql_* wrappers for mysql functions
14  *
15  * functions moved from globalfunctions.php: sql_connect, sql_disconnect, sql_query
16  */
17
18 $MYSQL_CONN = 0;
19
20 if (function_exists('mysql_query') && !function_exists('sql_fetch_assoc'))
21 {
22         /**
23          *Errors before the database connection has been made
24          */
25         function startUpError($msg, $title) {
26                 if (!defined('_CHARSET')) {
27                         define('_CHARSET', 'UTF-8');
28                 }
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"');
31                 }
32                 sendContentType('text/html','',_CHARSET);
33                 ?>
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>
37         <body>
38                 <h1><?php echo htmlspecialchars($title,ENT_QUOTES)?></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                 if ($mysql_database) mysql_select_db($mysql_database,$CONN);
53
54                 return $CONN;
55         }
56         
57         /**
58           * Connects to mysql server
59           */
60         function sql_connect() {
61                 global $MYSQL_HOST, $MYSQL_USER, $MYSQL_PASSWORD, $MYSQL_DATABASE, $MYSQL_CONN;
62
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');
65
66 // <add for garble measure>
67                 if (defined('_CHARSET')){
68                         $charset  = _CHARSET;
69                 }else{
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.
74                         // $charset = "ujis";
75                         // $charset = "utf8";
76                 }
77                 sql_set_charset_jp($charset);
78 // </add for garble measure>*/
79
80                 return $MYSQL_CONN;
81         }
82
83         /**
84           * disconnects from SQL server
85           */
86         function sql_disconnect($conn = false) {
87                 global $MYSQL_CONN;
88                 if (!$conn) $conn = $MYSQL_CONN;
89                 @mysql_close($conn);
90         }
91         
92         function sql_close($conn = false) {
93                 global $MYSQL_CONN;
94                 if (!$conn) $conn = $MYSQL_CONN;
95                 @mysql_close($conn);
96         }
97         
98         /**
99           * executes an SQL query
100           */
101         function sql_query($query, $conn = false) {
102                 global $SQLCount,$MYSQL_CONN;
103                 if (!$conn) $conn = $MYSQL_CONN;
104                 $SQLCount++;
105                 $res = mysql_query($query,$conn) or print("mySQL error with query $query: " . mysql_error($conn) . '<p />');
106                 return $res;
107         }
108         
109         /**
110           * executes an SQL error
111           */
112         function sql_error($conn = false)
113         {
114                 global $MYSQL_CONN;
115                 if (!$conn) $conn = $MYSQL_CONN;
116                 return mysql_error($conn);
117         }
118         
119         /**
120           * executes an SQL db select
121           */
122         function sql_select_db($db,$conn = false)
123         {
124                 global $MYSQL_CONN;
125                 if (!$conn) $conn = $MYSQL_CONN;
126                 return mysql_select_db($db,$conn);
127         }
128         
129         /**
130           * executes an SQL real escape 
131           */
132         function sql_real_escape_string($val,$conn = false)
133         {
134                 global $MYSQL_CONN;
135                 if (!$conn) $conn = $MYSQL_CONN;
136                 return mysql_real_escape_string($val,$conn);
137         }
138         
139         /**
140           * executes an PDO::quote() like escape, ie adds quotes arround the string and escapes chars as needed 
141           */
142         function sql_quote_string($val,$conn = false) {
143                 global $MYSQL_CONN;
144                 if (!$conn) $conn = $MYSQL_CONN;
145                 return "'".mysql_real_escape_string($val,$conn)."'";
146         }
147         
148         /**
149           * executes an SQL insert id
150           */
151         function sql_insert_id($conn = false)
152         {
153                 global $MYSQL_CONN;
154                 if (!$conn) $conn = $MYSQL_CONN;
155                 return mysql_insert_id($conn);
156         }
157         
158         /**
159           * executes an SQL result request
160           */
161         function sql_result($res, $row, $col)
162         {
163                 return mysql_result($res,$row,$col);
164         }
165         
166         /**
167           * frees sql result resources
168           */
169         function sql_free_result($res)
170         {
171                 return mysql_free_result($res);
172         }
173         
174         /**
175          * returns number of rows in SQL result
176          */
177         function sql_num_rows($res)
178         {
179                 return mysql_num_rows($res);
180         }
181         
182         /**
183          * returns number of rows affected by SQL query
184          */
185         function sql_affected_rows($conn = false)
186         {
187                 global $MYSQL_CONN;
188                 if (!$conn) $conn = $MYSQL_CONN;
189                 return mysql_affected_rows($conn);
190         }
191         
192         /**
193           * Get number of fields in result
194           */
195         function sql_num_fields($res)
196         {
197                 return mysql_num_fields($res);
198         }
199         
200         /**
201           * fetches next row of SQL result as an associative array
202           */
203         function sql_fetch_assoc($res)
204         {
205                 return mysql_fetch_assoc($res);
206         }
207         
208         /**
209           * Fetch a result row as an associative array, a numeric array, or both
210           */
211         function sql_fetch_array($res)
212         {
213                 return mysql_fetch_array($res);
214         }
215         
216         /**
217           * fetches next row of SQL result as an object
218           */
219         function sql_fetch_object($res)
220         {
221                 return mysql_fetch_object($res);
222         }
223         
224         /**
225           * Get a result row as an enumerated array
226           */
227         function sql_fetch_row($res)
228         {
229                 return mysql_fetch_row($res);
230         }
231         
232         /**
233           * Get column information from a result and return as an object
234           */
235         function sql_fetch_field($res,$offset = 0)
236         {
237                 return mysql_fetch_field($res,$offset);
238         }
239         
240         /**
241           * Get current system status (returns string)
242           */
243         function sql_stat($conn=false)
244         {
245                 global $MYSQL_CONN;
246                 if (!$conn) $conn = $MYSQL_CONN;
247                 return mysql_stat($conn);
248         }
249         
250         /**
251           * Returns the name of the character set
252           */
253         function sql_client_encoding($conn=false)
254         {
255                 global $MYSQL_CONN;
256                 if (!$conn) $conn = $MYSQL_CONN;
257                 return mysql_client_encoding($conn);
258         }
259         
260         /**
261           * Get SQL client version
262           */
263         function sql_get_client_info()
264         {
265                 return mysql_get_client_info();
266         }
267         
268         /**
269           * Get SQL server version
270           */
271         function sql_get_server_info($conn=false)
272         {
273                 global $MYSQL_CONN;
274                 if (!$conn) $conn = $MYSQL_CONN;
275                 return mysql_get_server_info($conn);
276         }
277         
278         /**
279           * Returns a string describing the type of SQL connection in use for the connection or FALSE on failure
280           */
281         function sql_get_host_info($conn=false)
282         {
283                 global $MYSQL_CONN;
284                 if (!$conn) $conn = $MYSQL_CONN;
285                 return mysql_get_host_info($conn);
286         }
287         
288         /**
289           * Returns the SQL protocol on success, or FALSE on failure. 
290           */
291         function sql_get_proto_info($conn=false)
292         {
293                 global $MYSQL_CONN;
294                 if (!$conn) $conn = $MYSQL_CONN;
295                 return mysql_get_proto_info($conn);
296         }
297
298         /**
299          * Get the name of the specified field in a result
300          */
301         function sql_field_name($res, $offset = 0)
302         {
303                 return mysql_field_name($res, $offset);
304         }
305
306 /**************************************************************************
307         Unimplemented mysql_* functions
308         
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)
322
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)
325
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))
328
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)
337
338 *******************************************************************/
339
340         /*
341          * for preventing I/O strings from auto-detecting the charactor encodings by MySQL
342          * since 3.62_beta-jp
343          * Jan.20, 2011 by kotorisan and cacher
344          * refering to their conversation below,
345          * http://japan.nucleuscms.org/bb/viewtopic.php?p=26581
346          * 
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.
349          */
350         function sql_set_charset_jp($charset) {
351                 switch(strtolower($charset)){\r
352                         case 'utf-8':
353                         case 'utf8':\r
354                                 $charset = 'utf8';\r
355                                 break;\r
356                         case 'euc-jp':
357                         case 'ujis':\r
358                                 $charset = 'ujis';\r
359                                 break;\r
360                         case 'gb2312':\r
361                                 $charset = 'gb2312';\r
362                                 break;
363                         /*\r
364                         case 'shift_jis':
365                         case 'sjis':\r
366                                 $charset = 'sjis';\r
367                                 break;
368                         */\r
369                         default:
370                                 $charset = 'latin1';\r
371                                 break;\r
372                 }
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);
378                 }
379                 return $res;
380         }
381 }