旧程序使用了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 ?>

在PHP7以上版本使用不了mysql扩展的更多相关文章

  1. PHP mysql扩展整理,操作数据库的实现过程分析

    相关文章:PHP mysqli扩展整理,包括面向过程和面向对象的比较\事务控制\批量执行\预处理   PHPmysqli扩展整理,包括面向过程和面向对象的比较\事务控制\批量执行\预处理 从某种程度上 ...

  2. Ubuntu 16.04 LAMP server 指南 - 配置 Apache2.4,PHP7,和MariaDB(而不是MySQL)

    翻译自:https://www.howtoforge.com/tutorial/install-apache-with-php-and-mysql-on-ubuntu-16-04-lamp/ 昨天在虚 ...

  3. PHPRAP v1.0.6 发布,修复因php7.1版本遗弃mcrypt扩展造成安装失败的BUG

    PHPRAP,是一个PHP轻量级开源API接口文档管理系统,致力于减少前后端沟通成本,提高团队协作开发效率,打造PHP版的RAP. 更新记录 [修复]修复因php7.1版本遗弃mcrypt扩展造成安装 ...

  4. Linux操作系统Centos7.2版本搭建Apache+PHP+Mysql环境

    对于在校大学生来说腾讯云1元主机很划算,所以就申请了一台,打算在上面学习下linux,使用版本为centos7.2版本.在服务器上比较推荐centos,此版本生命周期较长,而且网上有关centos的教 ...

  5. 从PHP5到PHP7的注意事项——PHP7全面删除Mysql扩展支持

    PHP删除了一些函数,其中是吓人的是两个: 1. ereg_* 系列的正则函数 2. mysql_* 系列的数据库连接函数 PHP7全面删除Mysql扩展支持,原本的mysql_*系列函数将在mysq ...

  6. phpstudy切换的php7.1版本php-cgi报错

    phpstudy切换的php7.1版本php-cgi报错 是因为没有装对应vc的插件. 下载地址:https://www.microsoft.com/zh-CN/download/details.as ...

  7. PHP7安装mysql扩展

    1.下载mysql扩展 http://git.php.net/?p=pecl/database/mysql.git;a=summary 2.解压后并使用phpize工具初始化(phpize一般在php ...

  8. MAC中PHP7.3安装mysql扩展

    1.下载mysql扩展http://git.php.net/?p=pecl/database/mysql.git;a=summary 2.解压tar xzvf mysql-d7643af.tar.gz ...

  9. Ubuntu 17.10 用 apt 搭建 lamp 环境、安装 phpmyadmin、redis 服务+扩展、mysql 扩展、开启错误提示、配置虚拟主机

    2018-02-24 13:50:30 更新: 个人喜欢相对原生又不太麻烦,所以用 apt 构建环境.不过,最近使用到现在记得出现过了 3 次 apache 或 mysql 服务器无法启动或无法连接的 ...

随机推荐

  1. nasm astrstr函数 x86

    xxx.asm: %define p1 ebp+8 %define p2 ebp+12 %define p3 ebp+16 section .text global dllmain export as ...

  2. Scrapy 项目:QuotesBot

    QuotesBot This is a Scrapy project to scrape quotes from famous people from http://quotes.toscrape.c ...

  3. glibc内存管理那些事儿

    本文转载自glibc内存管理那些事儿 Linux内存空间简介 32位Linux平台下进程虚拟地址空间分布如下图: 进程虚拟地址空间分布 图中,0xC0000000开始的最高1G空间是内核地址空间,剩下 ...

  4. HttpDns 原理是什么

    本文转载自HttpDns 原理是什么 什么是 DNS DNS(Domain Name System,域名系统),DNS 服务用于在网络请求时,将域名转为 IP 地址.能够使用户更方便的访问互联网,而不 ...

  5. 1080 Graduate Admission——PAT甲级真题

    1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...

  6. Winform 判断打印机是否可用,实现设置默认打印机功能

    Winform 判断打印机是否可用,实现设置默认打印机功能 http://www.cnblogs.com/zfanlong1314/p/3878563.html

  7. call、apply和bind的实现

    call方法 基础版, 只能修改指向,不能传参 Function.prototype.myCall = function(context) { // 获取调用者,这里为bar context.fn = ...

  8. Tomcat 安装Manager

    sudo apt-get install tomcat8-admin tomcat8-docs tomcat8-examplessudo vi /etc/tomcat8/tomcat-users.xm ...

  9. 第41天学习打卡(死锁 Lock synchronized与Lock的对比 线程协作 使用线程池)

    死锁 多个线程各自占有一些共享资源,并且互相等待其他线程占有的资源才能运行,而导致两个或者多个线程都在等待对方释放资源,都停止执行的情形.某一个同步块同时拥有"两个以上对象的锁"时 ...

  10. 微信小程序使用彩色图标(阿里巴巴 iconfont Symbol 的用法)微信小程序使用彩色图标(阿里巴巴 iconfont Symbol 的用法)

    前提 需要安装好 nodejs (略) 用于下载插件 安装插件 npm install mini-program-iconfont-cli --save-dev 初始化配置文件 npx iconfon ...