【问题标题】:How to solve PHP7.2 database error due to Count() changes如何解决由于 Count() 更改导致的 PHP7.2 数据库错误
【发布时间】:2018-05-15 07:18:36
【问题描述】:

我想我会测试新的 PHP 7.2 版本,看看它在我的网站上产生了多少问题(在此之前我一直在使用 7.1 没有问题),我注意到它似乎会中断 MySQL 数据库连接我的脚本之一的 Dp.php 文件出现以下错误:

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

它引用的代码是这个位:

    if (!count($dsn)) {
        return $parsed;
    }

我相信这与 7.2 (http://php.net/manual/en/migration72.incompatible.php) 中的“计数不可数类型”更改有关,这可能是由于“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;
    }

【问题讨论】:

  • 那么你期望 $dsn 在那个时候是什么?查看您的代码,您可以明确检查数组,因此显然它也可以是其他东西。也许您可以使检查更明确,例如if (!is_array($dsm) || count($dsm) == 0) 或查看this related question 以了解如何首先检查某事物是否可数。

标签: php mysql mysqli


【解决方案1】:

您在不是数组的值上使用count(),显然是为了检查它们是否为空。

请改用!== nullisset()

【讨论】:

  • 那么您认为将if (!count($dsn)) { 更改为if (null !== $dsn && !count($dsn)) { 可能有效吗?
猜你喜欢
  • 2021-12-30
  • 1970-01-01
  • 2020-08-01
  • 1970-01-01
  • 2021-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多