如何解决数据库类中的错误计数php

宝拉·托伦斯(Paula Torrens)

我无法解决此问题,请帮助我。

警告:count():参数必须是在第74行的/home/cabox/workspace/Datingmash/app/Class/Database.php中实现Countable的数组或对象

第74行:

public function bind($para, $value)
{   
    $this->parameters[count($this->parameters)] = ":" . $para . "\x7F" . utf8_encode($value);
}

这是数组

array(1) { [0]=> string(29) ":user_api_id1634508113362112" }

完整代码:

<?php

namespace App\Classes;
use PDO;

class Database extends DatabaseLogs {
    private $pdo;
    private $sQuery;
    private $bConnected = false;
    private $parameters;

    public function __construct(){
        $this->Connect();
        $this->parameters = array();
    }

    private function Connect(){
        $dsn = 'mysql:dbname='.APP_DATABASE_NAME.';host='.APP_DATABASE_HOSTNAME.'';
        try {
            $this->pdo = new PDO($dsn, APP_DATABASE_USERNAME, APP_DATABASE_PASSWORD, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
            $this->bConnected = true;
        }
        catch (PDOException $e) {
            echo $this->ExceptionLog($e->getMessage());
            die();
        }
    }

    public function CloseConnection(){
        $this->pdo = null;
    }

    private function Init($query,$parameters = "")
        {
        # Connect to database
        if(!$this->bConnected) { $this->Connect(); }
        try {
                # Prepare query
                $this->sQuery = $this->pdo->prepare($query);

                # Add parameters to the parameter array 
                $this->bindMore($parameters);
                # Bind parameters
                if(!empty($this->parameters)) {
                    foreach($this->parameters as $param)
                    {
                        $parameters = explode("\x7F",$param);
                        $this->sQuery->bindParam($parameters[0],$parameters[1]);
                    }       
                }
                # Execute SQL 
                $this->success = $this->sQuery->execute();      
            }
            catch(PDOException $e)
            {
                    # Write into log and display Exception
                    $this->ExceptionLog($e->getMessage(), $query );
            }
            # Reset the parameters
            $this->parameters = array();
        }

       /**
    *   @void 
    *
    *   Add the parameter to the parameter array
    *   @param string $para  
    *   @param string $value 
    */  
        public function bind($para, $value)
        {   
            $this->parameters[count($this->parameters)] = ":" . $para . "\x7F" . utf8_encode($value);
        }
       /**
    *   @void
    *   
    *   Add more parameters to the parameter array
    *   @param array $parray
    */  
        public function bindMore($parray)
        {
            if(empty($this->parameters) && is_array($parray)) {
                $columns = array_keys($parray);
                foreach($columns as $i => &$column) {
                    $this->bind($column, $parray[$column]);
                }
            }
        }
       /**
    *       If the SQL query  contains a SELECT or SHOW statement it returns an array containing all of the result set row
    *   If the SQL statement is a DELETE, INSERT, or UPDATE statement it returns the number of affected rows
    *
    *       @param  string $query
    *   @param  array  $params
    *   @param  int    $fetchmode
    *   @return mixed
    */          
        public function query($query,$params = null, $fetchmode = PDO::FETCH_ASSOC)
        {
            $query = trim($query);
            $this->Init($query,$params);
            $rawStatement = explode(" ", $query);

            # Which SQL statement is used 
            $statement = strtolower($rawStatement[0]);

            if ($statement === 'select' || $statement === 'show') {
                return $this->sQuery->fetchAll($fetchmode);
            }
            elseif ( $statement === 'insert' ||  $statement === 'update' || $statement === 'delete' ) {
                return $this->sQuery->rowCount();   
            }   
            else {
                return NULL;
            }
        }

      /**
       *  Returns the last inserted id.
       *  @return string
       */   
        public function lastInsertId() {
            return $this->pdo->lastInsertId();
        }   

       /**
    *   Returns an array which represents a column from the result set 
    *
    *   @param  string $query
    *   @param  array  $params
    *   @return array
    */  
        public function column($query,$params = null)
        {
            $this->Init($query,$params);
            $Columns = $this->sQuery->fetchAll(PDO::FETCH_NUM);     

            $column = null;
            foreach($Columns as $cells) {
                $column[] = $cells[0];
            }
            return $column;

        }   
       /**
    *   Returns an array which represents a row from the result set 
    *
    *   @param  string $query
    *   @param  array  $params
    *       @param  int    $fetchmode
    *   @return array
    */  
        public function row($query,$params = null,$fetchmode = PDO::FETCH_ASSOC)
        {               
            $this->Init($query,$params);
            return $this->sQuery->fetch($fetchmode);            
        }
       /**
    *   Returns the value of one single field/column
    *
    *   @param  string $query
    *   @param  array  $params
    *   @return string
    */  
        public function single($query,$params = null)
        {
            $this->Init($query,$params);
            return $this->sQuery->fetchColumn();
        }
       /**  
    * Writes the log and returns the exception
    *
    * @param  string $message
    * @param  string $sql
    * @return string
    */
    private function ExceptionLog($message , $sql = "")
    {
        $exception  = 'Unhandled Exception. <br />';
        $exception .= $message;
        $exception .= "<br /> You can find the error back in the log.";
        if(!empty($sql)) {
            # Add the Raw SQL to the Log
            $message .= "\r\nRaw SQL : "  . $sql;
        }
            # Write into log
            $this->log->write($message);
        throw new Exception($message);
        #return $exception;
    }           
}
?>
图凡·巴里斯·耶尔德勒姆(Tufan Baris Yildirim)

首先,您不需要传递count($this->paramters)为索引,

如果$this->paramters是数组,以下代码将正常工作

public function bind($para, $value)
{   
    $this->parameters[] = ":" . $para . "\x7F" . utf8_encode($value);
}

如果仍然要使用它,请确保$this->parameters始终是一个数组,或者只是即时修复它,请使用类似

public function bind($para, $value)
{   
    if(!is_array($this->parameters)){ $this->parameters = array(); }

    $this->parameters[ count($this->parameters) ] = ":" . $para . "\x7F" . utf8_encode($value);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何解决 Laravel 中的数据库更新错误

在数据库中插入数据时如何解决SQLiteException错误

如何解决错误的SQLite数据库路径?

如何解决数据库连接错误[SequelizeConnectionError]?

如何解决 SQLite 错误 5:数据库被锁定?

如何解决错误读取数据库:mongo DB中未授权

在Django的models.py中创建数据库时,如何解决迁移错误?

如何解决数据库关系中的循环问题?

如何解决远程数据库上的Firebird“不可用数据库”错误?

尝试访问localhost中的数据库时,如何解决“错误:服务器不支持SSL连接”?

在ASP.NET Core 3.1中扩展IdentityUser之后,如何解决EF Core数据库错误?

如何解决Key中的FireBase数据库禁用字符或解决方法

如何解决此“ ORA-01109:数据库未打开”错误?

Apache未启动如何解决此错误; Localhost数据库无法正常工作

如何解决错误的数据库供应商代码17

如何解决close()从未在数据库错误时显式调用

如何解决空数据库表上的乐观并发异常错误

加载数据库页面(Flask / Heroku)时如何解决内部服务器错误?

如何解决这个laravel migrate错误创建数据库表?

清洁架构-如何解决数据库事务?

如何解决我的数据库问题?(映射异常)

您如何解决关系数据库悖论?

尝试在数据库中插入日期时如何解决“第1行“ StartDate”列的错误日期值:“ 2020”错误”

如何解决无法解析H2数据库中的“ DATE”常量问题?

如何解决 java.sql.SQLException: 在数据库与 JDBC API 的集成中?

从数据库中获取空字符串时如何解决?

如何解决:“在 Heroku 上部署后数据库中的项目丢失”

如何解决Airflow Scheduler中的数据库连接无效警告?

如何解决ErrorException:未定义的索引:laravel中的数据库?