在PHP7以上版本使用不了mysql扩展

旧程序使用了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  */
  5 if (!function_exists('mysql_connect')) {
  6     if (!function_exists('mysqli_connect')) 
  7     {
  8         die("在PHP7.0以上版本兼容使用mysql扩展的旧程序,请先开启mysqli扩展!");
  9     }
 10 
 11     function mysql_connect(string $server,  $username,  $password, $new_link = false, $client_flags = 0)
 12     {
 13         $port = '3306';
 14         if (strpos($server, ':') !== false) {
 15             $host_port = explode(':', $server);
 16             $port = $host_port[1];
 17         }
 18         return mysqli_connect($server, $username, $password, '', $port);
 19     }
 20 
 21     function link_identifier_of(&$link_identifier = null)
 22     {
 23         if (is_null($link_identifier)) {
 24             $vars = array_merge($GLOBALS, get_defined_vars());
 25             $vars_len = count($vars);
 26             if ($vars_len > 0) {
 27                 foreach ($vars as $var) {
 28                     $type =  gettype($var);
 29                     if ($type == 'object' && $var instanceof mysqli) {
 30                         $link_identifier = $var;
 31                         break;
 32                     }
 33                 }
 34             }
 35         }
 36     }
 37 
 38     function mysql_error($link_identifier = null)
 39     {
 40         link_identifier_of($link_identifier);
 41 
 42         if (is_null($link_identifier)) {
 43             return mysqli_connect_error();
 44         }
 45 
 46         return mysqli_error($link_identifier);
 47     }
 48 
 49     function mysql_errno($link_identifier = null)
 50     {
 51         link_identifier_of($link_identifier);
 52 
 53         if (is_null($link_identifier)) {
 54             return mysqli_connect_errno();
 55         }
 56 
 57         return mysqli_errno($link_identifier);
 58     }
 59 
 60 
 61 
 62     function mysql_selectdb($database_name, $link_identifier)
 63     {
 64         return mysql_select_db($database_name, $link_identifier);
 65     }
 66 
 67     function mysql_select_db($database_name, $link_identifier = null)
 68     {
 69         link_identifier_of($link_identifier);
 70         return mysqli_select_db($link_identifier, $database_name);
 71     }
 72 
 73     function mysql_query($query, $link_identifier = null)
 74     {
 75         link_identifier_of($link_identifier);
 76         $resultmode = MYSQLI_STORE_RESULT;
 77         return mysqli_query($link_identifier, $query, $resultmode);
 78     }
 79 
 80     define('MYSQL_BOTH', MYSQLI_BOTH);
 81     define('MYSQL_ASSOC', MYSQLI_ASSOC);
 82     define('MYSQL_NUM', MYSQLI_NUM);
 83 
 84     function mysql_fetch_array($result, $result_type = MYSQL_BOTH)
 85     {
 86         return mysqli_fetch_array($result, $result_type);
 87     }
 88 
 89     function mysql_fetch_assoc($result)
 90     {
 91         return mysql_fetch_array($result, MYSQL_ASSOC);
 92     }
 93 
 94     function mysql_fetch_row($result)
 95     {
 96         return mysql_fetch_array($result, MYSQL_NUM);
 97     }
 98 
 99     function mysql_fetch_object($result, $class_name = 'stdClass', $params = null)
100     {
101         if (!$params) {
102             $params = array();
103         }
104 
105         return mysqli_fetch_object($result, $class_name, $params);
106     }
107 
108     function mysql_fetch_lengths($result)
109     {
110         return mysqli_fetch_lengths($result);
111     }
112 
113     function mysql_fetch_field($result, $field_offset = 0)
114     {
115         return mysqli_fetch_field_direct($result, $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         return mysqli_close($link_identifier);
225     }
226 
227     function mysql_pconnect($server, $username, $password, $client_flags = null)
228     {
229         return mysql_connect($server, $username, $password, true, 0);
230     }
231 
232     function mysql_stat($link_identifier = null)
233     {
234         link_identifier_of($link_identifier);
235         return mysqli_stat($link_identifier);
236     }
237 
238     function mysql_affected_rows($link_identifier = null)
239     {
240         link_identifier_of($link_identifier);
241         return mysqli_affected_rows($link_identifier);
242     }
243 
244     function mysql_client_encoding($link_identifier = null)
245     {
246         link_identifier_of($link_identifier);
247         return mysqli_client_encoding($link_identifier);
248     }
249 
250     function mysql_create_db($database_name, $link_identifier)
251     {
252         link_identifier_of($link_identifier);
253         $query = "create database `$database_name`  default character set = 'utf8' ";
254         $resultmode = MYSQLI_STORE_RESULT;
255         return mysqli_query($link_identifier, $query, $resultmode);
256     }
257 
258     function mysql_data_seek($result,  $row_number)
259     {
260         return mysqli_data_seek($result, $row_number);
261     }
262 
263 
264     function mysql_db_name($result, $row, $field = null)
265     { 
266         if (is_array($result)) {
267             return $result["$row"];
268         }
269         mysql_data_seek($result, $row);
270         $row = mysql_fetch_row($result);
271         $dbname = $row[0];
272         return $dbname;
273     }
274 
275     function mysql_db_query($database, $query, $link_identifier = null)
276     { 
277         link_identifier_of($link_identifier);
278         mysqli_select_db($link_identifier,$database);
279         mysqli_query($link_identifier,$query);
280     }
281 
282     function mysql_drop_db($database_name, $link_identifier)
283     { 
284         link_identifier_of($link_identifier);
285         $result = mysqli_query($link_identifier, "drop `$database_name`");
286         
287         return $result;
288     }
289 
290     function mysql_escape_string($unescaped_string, $link_identifier = null)
291     {
292         link_identifier_of($link_identifier);
293         return mysqli_escape_string($link_identifier, $unescaped_string);
294     }
295 
296 
297     function mysql_real_escape_string($unescaped_string, $link_identifier = null)
298     {
299         link_identifier_of($link_identifier);
300         return mysqli_real_escape_string($link_identifier, $unescaped_string);
301     }
302 
303     function mysql_get_client_info()
304     {
305         return mysqli_get_client_info();
306     }
307     function mysql_get_host_info($link_identifier = null)
308     {
309         link_identifier_of($link_identifier);
310         return mysqli_get_host_info($link_identifier);
311     }
312 
313     function mysql_get_proto_info($link_identifier = null)
314     {
315         link_identifier_of($link_identifier);
316         return mysqli_get_proto_info($link_identifier);
317     }
318 
319     function mysql_get_server_info($link_identifier = null)
320     {
321         link_identifier_of($link_identifier);
322         return mysqli_get_server_info($link_identifier);
323     }
324 
325     function mysql_info($link_identifier = null)
326     {
327         link_identifier_of($link_identifier);
328         return mysqli_info($link_identifier);
329     }
330 
331     function mysql_insert_id($link_identifier = null)
332     {
333         link_identifier_of($link_identifier);
334         return  mysqli_insert_id($link_identifier);
335     }
336 
337     function mysql_list_dbs($link_identifier = null)
338     {
339         link_identifier_of($link_identifier);
340         return mysqli_query($link_identifier, 'show databases');
341     }
342 
343     function mysql_list_fields($database_name, $table_name, $link_identifier = null)
344     {
345         link_identifier_of($link_identifier);
346         $result = mysqli_query($link_identifier, "DESCRIBE `$database_name`.`$table_name`");
347         $column = array();
348         while ($arr = mysqli_fetch_assoc($result)) {
349             $column[] = $arr['Field'];
350         }
351 
352         return $column;
353     }
354 
355     function mysql_num_fields($result)
356     {
357         if ($result instanceof object) {
358             return mysqli_num_fields($result);
359         }
360 
361         if (is_array($result)) {
362             return count($result);
363         }
364 
365         return 0;
366     }
367 
368     function mysql_list_processes($link_identifier = null)
369     {
370         link_identifier_of($link_identifier);
371         $result = mysqli_query($link_identifier, "show processlist");
372         return $result;
373     }
374 
375     function mysql_list_tables($database, $link_identifier = null)
376     {
377         link_identifier_of($link_identifier);
378         mysqli_query($link_identifier, "use `$database`");
379         $result = mysqli_query($link_identifier, "show tables");
380         return $result;
381     }
382 
383 
384     function mysql_num_rows($result)
385     {
386         if (is_array($result)) {
387             return count($result);
388         }
389 
390         return mysqli_num_rows($result);
391     }
392 
393     function mysql_ping($link_identifier = null)
394     {
395         link_identifier_of($link_identifier);
396         return mysqli_ping($link_identifier);
397     }
398 
399     function mysql_result($result, $row = 0, $field = 0)
400     {
401         mysql_data_seek($result, $row);
402         $arr = mysql_fetch_array($result);
403         $column_result = $arr["$field"];
404         if ($row != 0) {
405             mysql_data_seek($result, 0);
406         }
407 
408         return $column_result;
409     }
410 
411     function mysql_set_charset($charset, $link_identifier = null)
412     {
413         link_identifier_of($link_identifier);
414         return mysqli_set_charset($link_identifier, $charset);
415     }
416 
417     function mysql_tablename($result, $i=0)
418     {
419         if (is_array($result)) {
420             return $result["$i"];
421         }
422 
423         mysql_data_seek($result, $i);
424         $row = mysql_fetch_row($result);
425         $tablename = $row[0];
426 
427         return $tablename;
428     }
429 
430     function mysql_thread_id($link_identifier = null)
431     {
432         link_identifier_of($link_identifier);
433         return mysqli_thread_id($link_identifier);
434     }
435 
436     function mysql_unbuffered_query($query, $link_identifier = null)
437     {
438         link_identifier_of($link_identifier);
439         return mysqli_query($link_identifier, $query);
440     }
441 }
442 ?>
原文地址:https://www.cnblogs.com/dreamyoung/p/14638034.html