Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何解决Count()更改导致的PHP7.2数据库错误_Php_Mysql_Mysqli - Fatal编程技术网

如何解决Count()更改导致的PHP7.2数据库错误

如何解决Count()更改导致的PHP7.2数据库错误,php,mysql,mysqli,Php,Mysql,Mysqli,我想我应该测试新的PHP7.2版本,看看它在我的网站上造成了多少问题(在此之前我一直使用7.1,没有问题),我注意到它似乎破坏了我的一个脚本Dp.PHP文件中的MySQL数据库连接,出现了以下错误: 发生异常:count():参数必须是在/./Db.php的第57行实现可数的数组或对象 它引用的代码是此位: if (!count($dsn)) { return $parsed; } 我相信这与7.2()中的“计数不可数类型”更改有关,这可能是由于“null”值造

我想我应该测试新的PHP7.2版本,看看它在我的网站上造成了多少问题(在此之前我一直使用7.1,没有问题),我注意到它似乎破坏了我的一个脚本Dp.PHP文件中的MySQL数据库连接,出现了以下错误:

发生异常:count():参数必须是在/./Db.php的第57行实现可数的数组或对象

它引用的代码是此位:

    if (!count($dsn)) {
        return $parsed;
    }
我相信这与7.2()中的“计数不可数类型”更改有关,这可能是由于“null”值造成的,但我不是PHP专家,也不知道如何修复它。以下是与Dp.php文件中的$dsn相关的完整代码块:

class Censura_Db
{
    protected $connected_server_info; //cache for server information

    public static function factory($dsn, $options = false)
    {
        $class = new self($dsn, $options);
        return $class;
    }

    public static function parseDSN($dsn)
    {
        $parsed = array();
        if (is_array($dsn)) {
            $dsn = array_merge($parsed, $dsn);
            if (!$dsn['dbsyntax']) {
                $dsn['dbsyntax'] = $dsn['phptype'];
            }
            return $dsn;
        }
        // Find phptype and dbsyntax
        if (($pos = strpos($dsn, '://')) !== false) {
            $str = substr($dsn, 0, $pos);
            $dsn = substr($dsn, $pos + 3);
        } else {
            $str = $dsn;
            $dsn = null;
        }
        // Get phptype and dbsyntax
        // $str => phptype(dbsyntax)
        if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) {
            $parsed['phptype'] = $arr[1];
            $parsed['dbsyntax'] = !$arr[2] ? $arr[1] : $arr[2];
        } else {
            $parsed['phptype'] = $str;
            $parsed['dbsyntax'] = $str;
        }
        if (!count($dsn)) {
            return $parsed;
        }
        // Get (if found): username and password
        // $dsn => username:password@protocol+hostspec/database
        if (($at = strrpos($dsn, '@')) !== false) {
            $str = substr($dsn, 0, $at);
            $dsn = substr($dsn, $at + 1);
            if (($pos = strpos($str, ':')) !== false) {
                $parsed['username'] = rawurldecode(substr($str, 0, $pos));
                $parsed['password'] = rawurldecode(substr($str, $pos + 1));
            } else {
                $parsed['username'] = rawurldecode($str);
            }
        }
        // Find protocol and hostspec
        // $dsn => proto(proto_opts)/database
        if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match)) {
            $proto = $match[1];
            $proto_opts = $match[2] ? $match[2] : false;
            $dsn = $match[3];
            // $dsn => protocol+hostspec/database (old format)
        } else {
            if (strpos($dsn, '+') !== false) {
                list($proto, $dsn) = explode('+', $dsn, 2);
            }
            if (strpos($dsn, '//') === 0
                && strpos($dsn, '/', 2) !== false
                && $parsed['phptype'] == 'oci8'
            ) {
                //oracle's "Easy Connect" syntax:
                //"username/password@[//]host[:port][/service_name]"
                //e.g. "scott/tiger@//mymachine:1521/oracle"
                $proto_opts = $dsn;
                $dsn = null;
            } elseif (strpos($dsn, '/') !== false) {
                list($proto_opts, $dsn) = explode('/', $dsn, 2);
            } else {
                $proto_opts = $dsn;
                $dsn = null;
            }
        }
        // process the different protocol options
        $parsed['protocol'] = (!empty($proto)) ? $proto : 'tcp';
        $proto_opts = rawurldecode($proto_opts);
        if (strpos($proto_opts, ':') !== false) {
            list($proto_opts, $parsed['port']) = explode(':', $proto_opts);
        }
        if ($parsed['protocol'] == 'tcp') {
            $parsed['hostspec'] = $proto_opts;
        } elseif ($parsed['protocol'] == 'unix') {
            $parsed['socket'] = $proto_opts;
        }
        // Get dabase if any
        // $dsn => database
        if ($dsn) {
            // /database
            if (($pos = strpos($dsn, '?')) === false) {
                $parsed['database'] = $dsn;
                // /database?param1=value1&param2=value2
            } else {
                $parsed['database'] = substr($dsn, 0, $pos);
                $dsn = substr($dsn, $pos + 1);
                if (strpos($dsn, '&') !== false) {
                    $opts = explode('&', $dsn);
                } else { // database?param1=value1
                    $opts = array($dsn);
                }
                foreach ($opts as $opt) {
                    list($key, $value) = explode('=', $opt);
                    if (!isset($parsed[$key])) {
                        // don't allow params overwrite
                        $parsed[$key] = rawurldecode($value);
                    }
                }
            }
        }
        return $parsed;
    }
您正在对非数组的值使用
count()
,显然是为了检查它们是否为null


使用
!==null
isset()

那么您希望$dsn在这一点上是什么呢?查看您的代码,您得到了对数组的显式检查,因此显然它也可以是其他内容。也许你可以让检查更加明确,例如
if(!is_array($dsm)|count($dsm)==0)
,或者看看如何首先检查某个东西是否可数。那么你认为将
if(!count($dsn)){
更改为
if(null!=$dsn&!count($dsn)){
可能会起作用吗?