Jump to content
Fórum Script Brasil
  • 0

Classe de conexão MySQLi


lcerbaro

Question

Olá, estou criando uma classe de conexão usando a extensão MySQLi, porém estou com bastante dificuldades de concluir.

Gostaria da ajuda e experiência de vocês, obrigado.

<?php
/* /Classes/SQLi.php */
class Sql extends MySQLi
{
    private $host, $user, $pass, $database;
    
    protected $db = null;
    
    public $id;

    public function __construct($host, $user, $pass, $database)
    {
        $this->host = $host;
        $this->user = $user;
        $this->pass = $pass;
        $this->database = $database;

        //parent::__construct($host,$user,$pass,$database);
        //$this->init();
        //$this->real_connect();
        
        $this->getInstance();
    }
    
    public function getInstance()
    {
        
        if ($this->db === null)
        {
            parent::__construct($this->host,$this->user,$this->pass,$this->database);
        }
        return $this;
    }
    
    public function query($query)
    {
        $db = $this->getInstance();
        
        //$query = $db->real_escape_string($query);
        
        $result = $db->real_query($query);
        
        //$res = $this->store_result();

        $this->id = $this->insert_id;

        return new SqlR($db);
        
    }
    
}

class SqlR extends MySQLi_Result
{
    protected $db = null;
    
    public function __construct($db)
    {
        $this->db = $db;
        
        $this->getInstance();
    }
    
    public function getInstance()
    {
        if($this->db === null)
        {
            parent::__construct();
        }
        return $this;
    
    }
    
    public function fetch()
    {
        $db = $this->getInstance();
        
        return $db->fetch_object();
    }
    
    public function rows()
    {
        $db = $this->getInstance();
        
        return $db->num_rows;
    }
    
    public function rewind()
    {
        $db = $this->getInstance();
        
        return $db->data_seek(0);
    } 
    
    public function seek($i)
    {
        $db = $this->getInstance();
        
        return $db->data_seek($i);
    }
}

?>
Aqui um exemplo de uso:
<?php
/* /page/index.php */
ini_set('display_errors', true);
error_reporting(-1);
require_once "../Classes/SQLi.php";

$db = new Sql("localhost","root","","mydb");

$sql= "select * from tablename";
$res = $db->query($sql);

while($rows = $res->fetch())
    var_dump($rows);

?>

E o erro:

( ! ) Fatal error: Call to undefined method mysqli_result::fetch() in C:\wamp\www\newtry\index.php on line 12
Edited by lcerbaro
Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...