让PHP7兼容老版本的php5.5.x或者5.6下的mysql_connect的处理代码

星期一, 2019-09-09 | Author: Lee | linux, php | 3,340 views

1.昨天折腾了下服务器,把系统从centos6.5更新到7.6,也把对应的php版本升级到了php7.0.x系列

2.好多年没有再怎么写php代码了,以前写的也是在php5.5.x的版本写的,升级后发现程序无法使用,郁闷ing

3.跟踪下 /var/opt/remi/php70/log/php-fpm 下的 www-error.log发现是 php7已经不支持mysql_connect

4.网上找了下兼容的代码新增了 mysql_num_rows的函数的处理,我以前的老代码又可以欢快的跑起来了,记录下,也方便需要的朋友

$dbhost = DATA_HOST;
$dbport = 3306;
$dbuser = DATA_USERNAME;
$dbpass = DATA_PASSWORD;
$dbname = DATA_NAME;
if(!function_exists('mysql_connect')){
    function mysql_connect($dbhost, $dbuser, $dbpass){
        global $dbport;
        global $dbname;
        global $mysqli;
        $mysqli = mysqli_connect("$dbhost:$dbport", $dbuser, $dbpass, $dbname);
        return $mysqli;
        }
    function mysql_select_db($dbname){
        global $mysqli;
        return mysqli_select_db($mysqli,$dbname);
        }
    function mysql_fetch_array($result){
        return mysqli_fetch_array($result);
        }
    function mysql_fetch_assoc($result){
        return mysqli_fetch_assoc($result);
        }
    function mysql_fetch_row($result){
        return mysqli_fetch_row($result);
        }
    function mysql_query($query){
        global $mysqli;
        return mysqli_query($mysqli,$query);
        }
    function mysql_escape_string($data){
        global $mysqli;
        return mysqli_real_escape_string($mysqli, $data);
        }
    function mysql_real_escape_string($data){
        return mysql_real_escape_string($data);
        }
    function mysql_close(){
        global $mysqli;
        return mysqli_close($mysqli);
        }
    function mysql_num_rows($result){
        return mysqli_num_rows($result);
    }
}

Tags: , ,

文章作者: Lee

本文地址: https://www.pomelolee.com/1915.html

除非注明,Pomelo Lee文章均为原创,转载请以链接形式标明本文地址

No comments yet.

Leave a comment

Search

文章分类

Links

Meta