Mysql to MYSQLI connection (class)

Asked

Viewed 70 times

0

Would you like to make this connection on mysqli, could someone help me? I’m having some difficulties.

 <?php
Class mysql
{

    public $query;
    public $data;
    public $result;
    public $rows;
    protected $config;
    protected $host;
    protected $port;
    protected $user;
    protected $pass;
    protected $dbname;
    protected $con;

    public function __construct()
    {
        try
        {
            #array com dados do banco
            include 'database.conf.php';
            global $databases;
            $this->config = $databases['local'];
            # Recupera os dados de conexao do config
            $this->dbname = $this->config['dbname'];
            $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 = new mysqli( "$this->host", "$this->user", "$this->pass","$this->dbname");
            //@mysql_select_db( "$this->dbname" );
            if ( !$this->con )
            {
                throw new Exception( "Falha na conexão MySql com o banco [$this->dbname] em 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 = mysqli_query( $this->query );
            }
        }
        catch ( Exception $e )
        {
            echo $e->getMessage();
            exit;
        }
        return $this;
    }

    public function fetchAll()
    {
        $this->data = "";
        $this->rows = 0;
        while ( $row = @mysqli_fetch_array( $this->result, MYSQLI_ASSOC ) )
        {
            $this->data[] = $row;
        }
        if ( isset( $this->data[0] ) )
        {
            $this->rows = count( $this->data );
        }
        return $this->data;
    }

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

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

}

/* end file */

database.conf.php

<?php
global $databases;
$databases = array( 
    'local' => array
    (
            'host'=>'localhost',
            'port'=>3306,
            'dbname'=>'ok',
            'user'=>'root',
            'password'=>'root'
    )
);
  • Someone can help me?

  • Have you tried https://answall.com/questions/47880/comoractualizr-meu-c%C3%B3digo-mysql-para-mysqli or https://answall.com/questions/33622/qual%C3%A9-the-sure-way-to-connect-with-the-database-mysqli?

  • I tried friend, but I could not make the adaptation in this class that I put in the post. If it was only the simple connection I get. But I can’t adapt in the class above.

  • I did it really fast and I didn’t test it, but you can take a look https://hastebin.com/ibuqahowos.xml

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.