请帮助从mysql转换为mysqli吗?

时间:2019-06-25 00:16:44

标签: php mysql

我在PHP和MYSQL方面经验不足,并且遇到了确切的帮助问题,无法将MYSQL中的连接转换为MYSQLI,请遵循以下代码:

mysql.php

<?php
/* Adapter mysql */
Class mysql
{

    public $query;
    public $fetchAll;
    public $result;
    public $response;
    protected $config;
    protected $driver;
    protected $host;
    protected $port;
    protected $user;
    protected $pass;
    protected $dbname;
    protected $con;

    public function __construct( $config )
    {
        try
        {
            #array com dados do banco
            $this->config = $config;
            # Recupera os dados de conexao do config
            $this->dbname = $this->config['dbname'];
            $this->driver = $this->config['driver'];
            $this->host = $this->config['host'];
            $this->port = $this->config['port'];
            $this->user = $this->config['user'];
            $this->pass = $this->config['password'];
            # instancia e retorna objeto
            $this->con = @mysql_connect( "$this->host", "$this->user", "$this->pass" );
            @mysql_select_db( "$this->dbname" );
            if( !$this->con )
            {
                throw new Exception( "Falha na conex�o MySql com o banco [$this->dbname] em " . DATABASEDIR . "database.conf.php" );
            }
            else
            {
                return $this->con;
            }
        }
        catch( Exception $e )
        {
            echo $e->getMessage();
            exit;
        }
        return $this;
    }

    public function query( $query = '' )
    {
        try
        {
            if( $query == '' )
            {
                throw new Exception( 'mysql query: A query deve ser informada como parâmetro do método.' );
            }
            else
            {
                $this->query = $query;
                $this->result = @mysql_query( $this->query );
                if(!$this->result)
                {
                    $this->response = "Erro " .mysql_errno()." => ". mysql_error();
                }
                else
                {
                    $this->response = "success";
                }                
            }
        }
        catch( Exception $e )
        {
            echo $e->getMessage();
            exit;
        }
        return $this;
    }

    public function fetchAll()
    {
        $this->fetchAll = "";
        while( $row = @mysql_fetch_array( $this->result, MYSQL_ASSOC ) )
        {
            $this->fetchAll[] = $row;
        }
        return $this->fetchAll;
    }

    public function rowCount()
    {
        return @mysql_affected_rows();
    }

    public function limit( $limit, $offset )
    {
        return "LIMIT " . (int) $limit . "," . (int) $offset;
    }
}
/* end file */

**database.conf.php**

<?php

$databases = array(
    # MYSQL
    'default' => array
        (
        'driver' => 'mysql',
        'host' => 'localhost',
        'port' => 3306,
        'dbname' => 'dbname',
        'user' => 'root',
        'password' => '',
        'emailAdmin' => 'e-mail.com'// email para receber mensagens do chat
    )
);

Conexao.class

<?php

Class Conexao extends PDO
{

    protected $config;
    protected $driver;
    protected $sgbd;
    protected $host;
    protected $port;
    protected $user;
    protected $pass;
    protected $dbname;
    protected $strcon;
    protected $con;

    public function __construct( $config )
    {
        try
        {
            #array com dados do banco
            $this->config = $config;
            # Recuperando os dados de conexao do driver
            $this->dbname = $this->config['dbname'];
            $this->driver = $this->config['driver'];
            $this->sgbd = $this->config['sgbd'];
            $this->host = $this->config['host'];
            $this->port = $this->config['port'];
            $this->user = $this->config['user'];
            $this->pass = $this->config['password'];
            $this->strCon = "$this->sgbd:host=$this->host;port=$this->port;";
            # instancia e retorna objeto PDO
            $this->con = parent :: __construct( "$this->strCon dbname=$this->dbname", $this->user, $this->pass, array( PDO::ATTR_PERSISTENT => true ) );
            return $this->con;
        }
        catch( PDOException $e )
        {
            echo 'A Conexão falhou: ' . $e->getMessage();
            exit;
        }
    }

    public function close()
    {
        unset( $this->con );
        unset( $this->dbname );
        unset( $this->config );
    }
}
/* end file */

Error code

已经尝试将$ this-> con = @mysql_connect更改为$ link = new mysqli_connect

mysql.php

<?php
/* Adapter mysql */
Class mysql
{

    public $query;
    public $fetchAll;
    public $result;
    public $response;
    protected $config;
    protected $driver;
    protected $host;
    protected $port;
    protected $user;
    protected $pass;
    protected $dbname;
    protected $con;

    public function __construct( $config )
    {
        try
        {
            #array com dados do banco
            $this->config = $config;
            # Recupera os dados de conexao do config
            $this->dbname = $this->config['dbname'];
            $this->driver = $this->config['driver'];
            $this->host = $this->config['host'];
            $this->port = $this->config['port'];
            $this->user = $this->config['user'];
            $this->pass = $this->config['password'];
            # instancia e retorna objeto
            $this->con = @mysql_connect( "$this->host", "$this->user", "$this->pass" );
            @mysql_select_db( "$this->dbname" );
            if( !$this->con )
            {
                throw new Exception( "Falha na conex�o MySql com o banco [$this->dbname] em " . DATABASEDIR . "database.conf.php" );
            }
            else
            {
                return $this->con;
            }
        }
        catch( Exception $e )
        {
            echo $e->getMessage();
            exit;
        }
        return $this;
    }

    public function query( $query = '' )
    {
        try
        {
            if( $query == '' )
            {
                throw new Exception( 'mysql query: A query deve ser informada como parâmetro do método.' );
            }
            else
            {
                $this->query = $query;
                $this->result = @mysql_query( $this->query );
                if(!$this->result)
                {
                    $this->response = "Erro " .mysql_errno()." => ". mysql_error();
                }
                else
                {
                    $this->response = "success";
                }                
            }
        }
        catch( Exception $e )
        {
            echo $e->getMessage();
            exit;
        }
        return $this;
    }

    public function fetchAll()
    {
        $this->fetchAll = "";
        while( $row = @mysql_fetch_array( $this->result, MYSQL_ASSOC ) )
        {
            $this->fetchAll[] = $row;
        }
        return $this->fetchAll;
    }

    public function rowCount()
    {
        return @mysql_affected_rows();
    }

    public function limit( $limit, $offset )
    {
        return "LIMIT " . (int) $limit . "," . (int) $offset;
    }
}
/* end file */

**database.conf.php**

<?php

$databases = array(
    # MYSQL
    'default' => array
        (
        'driver' => 'mysql',
        'host' => 'localhost',
        'port' => 3306,
        'dbname' => 'dbname',
        'user' => 'root',
        'password' => '',
        'emailAdmin' => 'e-mail.com'// email para receber mensagens do chat
    )
);

Conexao.class

<?php

Class Conexao extends PDO
{

    protected $config;
    protected $driver;
    protected $sgbd;
    protected $host;
    protected $port;
    protected $user;
    protected $pass;
    protected $dbname;
    protected $strcon;
    protected $con;

    public function __construct( $config )
    {
        try
        {
            #array com dados do banco
            $this->config = $config;
            # Recuperando os dados de conexao do driver
            $this->dbname = $this->config['dbname'];
            $this->driver = $this->config['driver'];
            $this->sgbd = $this->config['sgbd'];
            $this->host = $this->config['host'];
            $this->port = $this->config['port'];
            $this->user = $this->config['user'];
            $this->pass = $this->config['password'];
            $this->strCon = "$this->sgbd:host=$this->host;port=$this->port;";
            # instancia e retorna objeto PDO
            $this->con = parent :: __construct( "$this->strCon dbname=$this->dbname", $this->user, $this->pass, array( PDO::ATTR_PERSISTENT => true ) );
            return $this->con;
        }
        catch( PDOException $e )
        {
            echo 'A Conexão falhou: ' . $e->getMessage();
            exit;
        }
    }

    public function close()
    {
        unset( $this->con );
        unset( $this->dbname );
        unset( $this->config );
    }
}
/* end file */

致命错误:未捕获错误:调用C:\ xampp \ htdocs \ ecommerce \ app \ database \ mysql.php:34中未定义的函数mysql_select_db()堆栈跟踪:#0 C:\ xampp \ htdocs \ ecommerce \ app \ class \ PHPFrodo.class.php(127):mysql-> __ construct(Array)#1 C:\ xampp \ htdocs \ ecommerce \ app \ class \ PHPFrodo.class.php(60):PHPFrodo-> database()# 2 C:\ xampp \ htdocs \ ecommerce \ app \ index.php(18):PHPFrodo-> __ construct()#3 C:\ xampp \ htdocs \ ecommerce \ index.php(106):Index->​​ __ construct()# 4 {main}在第34行的C:\ xampp \ htdocs \ ecommerce \ app \ database \ mysql.php中抛出

0 个答案:

没有答案