旧程序使用了mysql扩展,而新环境却是PHP7以上版本,不支持mysql扩展,办法是将旧程序中的mysql相关内容修改为mysqli或PDO代码。

但是涉及修改的量大,那则可以包含(include "mysql.functions.php")此实现了mysql扩展的所有方法的兼容文件。

这里用mysqli扩展实现了原mysql扩展的所有方法,旧程序直接包含此文件即可,可以无需修改再修改其它代码。

mysql.functions.php : 

 

  1 <?php
  2 /**
  3  * php7 不支持mysql扩展的情况下,旧程序运行在PHP7环境下,直接通过公用文件包含此mysql扩展相关的函数包即可。
  4  如果旧程序不想使用mysql扩展,但想使用mysql扩展的相同的函数,也可以使用此文件。
  5  */
  6 if (!function_exists('mysql_connect')) {
  7     function_exists('mysqli_connect') or die("要在没有mysql扩展的PHP中使用mysql相关函数,请先开启mysqli扩展!");    
  8 
  9     function mysql_connect($server,  $username,  $password, $new_link = false, $client_flags = 0)
 10     {
 11         $port = '3306';
 12         if (strpos($server, ':') !== false) {
 13             $host_port = explode(':', $server);
 14             $port = $host_port[1];
 15         }
 16         $link = mysqli_connect($server, $username, $password, '', $port);
 17         
 18         //mysqli不像mysql扩展那样在全局维护连接对象(mysql操作函数省略连接参数进行查询时会自动从全局作用域获取连接),所以这里手动添加一个到全局。
 19         if(!mysqli_connect_errno()){
 20             $GLOBALS['link_identifier'] = $link;
 21             return $link;
 22         }
 23         
 24         return false;
 25     }
 26 
 27     function link_identifier_of(&$link_identifier = null)
 28     {
 29         if (is_null($link_identifier)) {
 30             $link_identifier = !empty($GLOBALS['link_identifier']) ? $GLOBALS['link_identifier'] : null;
 31         }
 32     }
 33 
 34     function mysql_error($link_identifier = null)
 35     {
 36         if(empty($GLOBALS['link_identifier'])){
 37             return mysqli_connect_error();
 38         }
 39         
 40         link_identifier_of($link_identifier);
 41         return mysqli_error($link_identifier);
 42     }
 43 
 44     function mysql_errno($link_identifier = null)
 45     {        
 46 
 47         if(empty($GLOBALS['link_identifier'])){
 48             return mysqli_connect_errno();
 49         }
 50         
 51         link_identifier_of($link_identifier);
 52         return mysqli_errno($link_identifier);
 53     }
 54 
 55 
 56 
 57     function mysql_selectdb($database_name, $link_identifier=null)
 58     {
 59         return mysql_select_db($database_name, $link_identifier);
 60     }
 61 
 62     function mysql_select_db($database_name, $link_identifier = null)
 63     {
 64         link_identifier_of($link_identifier);
 65         return mysqli_select_db($link_identifier, $database_name);
 66     }
 67 
 68     function mysql_query($query, $link_identifier = null)
 69     {
 70         link_identifier_of($link_identifier);
 71         $resultmode = MYSQLI_STORE_RESULT;
 72         return mysqli_query($link_identifier, $query, $resultmode);
 73     }
 74 
 75     defined('MYSQL_BOTH') or define('MYSQL_BOTH', MYSQLI_BOTH);
 76     defined('MYSQL_ASSOC') or define('MYSQL_ASSOC', MYSQLI_ASSOC);
 77     defined('MYSQL_NUM') or define('MYSQL_NUM', MYSQLI_NUM);
 78 
 79     function mysql_fetch_array($result, $result_type = MYSQL_BOTH)
 80     {
 81         return is_object($result) ? mysqli_fetch_array($result, $result_type) : array();
 82     }
 83 
 84     function mysql_fetch_assoc($result)
 85     {
 86         return mysql_fetch_array($result, MYSQL_ASSOC);
 87     }
 88 
 89     function mysql_fetch_row($result)
 90     {
 91         return mysql_fetch_array($result, MYSQL_NUM);
 92     }
 93 
 94     function mysql_fetch_object($result, $class_name = 'stdClass', $params = null)
 95     {
 96         if (!$params) {
 97             $params = array();
 98         }
 99 
100         return mysqli_fetch_object($result, $class_name, $params);
101     }
102 
103     function mysql_fetch_lengths($result)
104     {
105         return mysqli_fetch_lengths($result);
106     }
107 
108     function mysql_fetch_field($result, $field_offset = null)
109     {
110         // fix
111         if(!isset($field_offset)){
112             return mysqli_fetch_field($result);
113         }
114         
115         return mysqli_fetch_field_direct($result, intval($field_offset));
116     }
117 
118     function mysql_field_name($result, $field_offset = 0)
119     {
120 
121         if (is_object($result)) {
122             $mysql_field = mysql_fetch_field($result, $field_offset);
123             if (is_object($mysql_field)) {
124                 return $mysql_field->name;
125             }
126         }
127 
128         if (is_array($result)) {
129             return $result["$field_offset"];
130         }
131 
132         return null;
133     }
134 
135     function mysql_field_len($result, $field_offset = 0)
136     {
137         $mysql_field = mysql_fetch_field($result, $field_offset);
138         if (is_object($mysql_field)) {
139             return $mysql_field->length;
140         }
141         return null;
142     }
143 
144 
145     function mysql_field_table($result, $field_offset = 0)
146     {
147         $mysql_field = mysql_fetch_field($result, $field_offset);
148         if (is_object($mysql_field)) {
149             return $mysql_field->table;
150         }
151         return null;
152     }
153 
154 
155     function mysql_field_type($result, $field_offset = 0)
156     {
157         $mysql_field = mysql_fetch_field($result, $field_offset);
158         if (is_object($mysql_field)) {
159             $type =  $mysql_field->type;
160 
161             switch ($type) {
162                 case MYSQLI_TYPE_VAR_STRING:
163                 case  MYSQLI_TYPE_STRING:
164                     $type = 'string';
165                     break;
166                 case MYSQLI_TYPE_LONG:
167                 case MYSQLI_TYPE_SHORT:
168                 case MYSQLI_TYPE_TINY:
169                 case MYSQLI_TYPE_LONGLONG:
170                     $type = 'int';
171                     break;
172                 case MYSQLI_TYPE_BLOB:
173                 case MYSQLI_TYPE_LONG_BLOB:
174                 case MYSQLI_TYPE_MEDIUM_BLOB:
175                 case MYSQLI_TYPE_TINY_BLOB:
176                     $type = 'blob';
177                     break;
178                 case MYSQLI_TYPE_CHAR:
179                     $type = 'char';
180                     break;
181                 default:
182                     $type = 'string';
183                     break;
184             }
185             return $type;
186         }
187         return null;
188     }
189 
190     function mysql_field_flags($result, $field_offset)
191     {
192         $mysql_field = mysql_fetch_field($result, $field_offset);
193         if (is_object($mysql_field)) {
194             //return $mysql_field->flags;
195         }
196         return null;
197     }
198 
199     function mysql_field_seek($result, $field_offset)
200     {
201         $mysql_field = mysql_fetch_field($result, $field_offset);
202         if (is_object($mysql_field)) {
203             //return $mysql_field->se;
204         }
205         return null;
206     }
207 
208     function mysql_free_result($result)
209     {
210         $flag = true;
211         try {
212             mysqli_free_result($result);
213         } catch (\Exception $e) {
214             $flag = false;
215         }
216 
217         return $flag;
218     }
219 
220 
221     function mysql_close($link_identifier = null)
222     {
223         link_identifier_of($link_identifier);
224         
225         if(!empty($GLOBALS['link_identifier'])){
226             unset($GLOBALS['link_identifier']);
227         }
228         return mysqli_close($link_identifier);
229     }
230 
231     function mysql_pconnect($server, $username, $password, $client_flags = null)
232     {
233         return mysql_connect($server, $username, $password, true, 0);
234     }
235 
236     function mysql_stat($link_identifier = null)
237     {
238         link_identifier_of($link_identifier);
239         return mysqli_stat($link_identifier);
240     }
241 
242     function mysql_affected_rows($link_identifier = null)
243     {
244         link_identifier_of($link_identifier);
245         return mysqli_affected_rows($link_identifier);
246     }
247 
248     function mysql_client_encoding($link_identifier = null)
249     {
250         link_identifier_of($link_identifier);
251         return mysqli_client_encoding($link_identifier);
252     }
253 
254     function mysql_create_db($database_name, $link_identifier)
255     {
256         link_identifier_of($link_identifier);
257         $query = "create database `$database_name`  default character set = 'utf8' ";
258         $resultmode = MYSQLI_STORE_RESULT;
259         return mysqli_query($link_identifier, $query, $resultmode);
260     }
261 
262     function mysql_data_seek($result,  $row_number)
263     {
264         return mysqli_data_seek($result, $row_number);
265     }
266 
267 
268     function mysql_db_name($result, $row, $field = null)
269     {
270         if (is_array($result)) {
271             return $result["$row"];
272         }
273         mysql_data_seek($result, $row);
274         $row = mysql_fetch_row($result);
275         $dbname = $row[0];
276         return $dbname;
277     }
278 
279     function mysql_db_query($database, $query, $link_identifier = null)
280     {
281         link_identifier_of($link_identifier);
282         mysqli_select_db($link_identifier,$database);
283         mysqli_query($link_identifier,$query);
284     }
285 
286     function mysql_drop_db($database_name, $link_identifier)
287     {
288         link_identifier_of($link_identifier);
289         $result = mysqli_query($link_identifier, "drop `$database_name`");
290 
291         return $result;
292     }
293 
294     function mysql_escape_string($unescaped_string, $link_identifier = null)
295     {
296         link_identifier_of($link_identifier);
297         return mysqli_escape_string($link_identifier, $unescaped_string);
298     }
299 
300 
301     function mysql_real_escape_string($unescaped_string, $link_identifier = null)
302     {
303         link_identifier_of($link_identifier);
304         return mysqli_real_escape_string($link_identifier, $unescaped_string);
305     }
306 
307     function mysql_get_client_info()
308     {
309         return mysqli_get_client_info();
310     }
311     function mysql_get_host_info($link_identifier = null)
312     {
313         link_identifier_of($link_identifier);
314         return mysqli_get_host_info($link_identifier);
315     }
316 
317     function mysql_get_proto_info($link_identifier = null)
318     {
319         link_identifier_of($link_identifier);
320         return mysqli_get_proto_info($link_identifier);
321     }
322 
323     function mysql_get_server_info($link_identifier = null)
324     {
325         link_identifier_of($link_identifier);
326         return mysqli_get_server_info($link_identifier);
327     }
328 
329     function mysql_info($link_identifier = null)
330     {
331         link_identifier_of($link_identifier);
332         return mysqli_info($link_identifier);
333     }
334 
335     function mysql_insert_id($link_identifier = null)
336     {
337         link_identifier_of($link_identifier);
338         return  mysqli_insert_id($link_identifier);
339     }
340 
341     function mysql_list_dbs($link_identifier = null)
342     {
343         link_identifier_of($link_identifier);
344         return mysqli_query($link_identifier, 'show databases');
345     }
346 
347     function mysql_list_fields($database_name, $table_name, $link_identifier = null)
348     {
349         link_identifier_of($link_identifier);
350         $result = mysqli_query($link_identifier, "DESCRIBE `$database_name`.`$table_name`");
351         $column = array();
352         while ($arr = mysqli_fetch_assoc($result)) {
353             $column[] = $arr['Field'];
354         }
355 
356         return $column;
357     }
358 
359     function mysql_num_fields($result)
360     {
361         if ($result instanceof object) {
362             return mysqli_num_fields($result);
363         }
364 
365         if (is_array($result)) {
366             return count($result);
367         }
368 
369         return 0;
370     }
371 
372     function mysql_list_processes($link_identifier = null)
373     {
374         link_identifier_of($link_identifier);
375         $result = mysqli_query($link_identifier, "show processlist");
376         return $result;
377     }
378 
379     function mysql_list_tables($database, $link_identifier = null)
380     {
381         link_identifier_of($link_identifier);
382         mysqli_query($link_identifier, "use `$database`");
383         $result = mysqli_query($link_identifier, "show tables");
384         return $result;
385     }
386 
387 
388     function mysql_num_rows($result)
389     {
390         if (is_array($result)) {
391             return count($result);
392         }
393 
394         return mysqli_num_rows($result);
395     }
396 
397     function mysql_ping($link_identifier = null)
398     {
399         link_identifier_of($link_identifier);
400         return mysqli_ping($link_identifier);
401     }
402 
403     function mysql_result($result, $row = 0, $field = 0)
404     {
405         mysql_data_seek($result, $row);
406         $arr = mysql_fetch_array($result);
407         $column_result = $arr["$field"];
408         if ($row != 0) {
409             mysql_data_seek($result, 0);
410         }
411 
412         return $column_result;
413     }
414 
415     function mysql_set_charset($charset, $link_identifier = null)
416     {
417         link_identifier_of($link_identifier);
418         return mysqli_set_charset($link_identifier, $charset);
419     }
420 
421     function mysql_tablename($result, $i=0)
422     {
423         if (is_array($result)) {
424             return $result["$i"];
425         }
426 
427         mysql_data_seek($result, $i);
428         $row = mysql_fetch_row($result);
429         $tablename = $row[0];
430 
431         return $tablename;
432     }
433 
434     function mysql_thread_id($link_identifier = null)
435     {
436         link_identifier_of($link_identifier);
437         return mysqli_thread_id($link_identifier);
438     }
439 
440     function mysql_unbuffered_query($query, $link_identifier = null)
441     {
442         link_identifier_of($link_identifier);
443         return mysqli_query($link_identifier, $query);
444     }
445 }

 

posted on 2021-04-09 17:16  Dream Young  阅读(1123)  评论(0编辑  收藏  举报