About MySQL functions

3

I have a question about some functions of MySQLi

In the conventional way we would do this:

public function Numrows($sql) {
      return mysqli_num_rows($sql);
}

And since I'm using extends mysqli I'm doing this:

public function dbNumRows($Query) {

    $sql = parent::prepare($Query);

    return $sql->num_rows();
}

What is the most correct way to return line numbers using PHP Oriented with class extends mysqli ?

Full class connection

<?php

class Connect extends mysqli {

    public $db_connection, $db_hostname, $db_username, $db_password, $db_database;

    public function __construct() {
        $this->db_hostname = DB_HOSTNAME;
        $this->db_username = DB_USERNAME;
        $this->db_password = DB_PASSWORD;
        $this->db_database = DB_DATABASE;

        $this->connectMe();
    }

    private function connectMe() {
        $this->db_connection = @$this->connect($this->db_hostname, $this->db_username, $this->db_password, $this->db_database);

        if ($this->connect_error) {
            die("Falha na tentativa de se conectar com o servidor: " . $this->connect_error);
        }
    }

    public function dbExecute($Query) {
        $Result = $this->query($Query);

        if ($this->error) {
            die("Erro no comando: $Query");
        }

        return $Result;
    }

    public function dbNumRows($Query) {
       return $this->prepare($Query)->num_rows();
    }

}

Att.

    
asked by anonymous 22.05.2015 / 07:55

2 answers

2

If you are inheriting from the mysqli class all available methods will be in your new class. You do not need to create new methods for this:

<?php

class MeuBanco extends mysqli{

    // ...

}

$db = new MeuBanco();
$db->prepare('SELECT * FROM test')->num_rows();

If you want to use the methods of mysqli within your class you do not need to use parent:: , you can use $this as if it were a method of the class itself.

public function dbNumRows($query) 
{
    return $this->prepare($query)->num_rows();
}

Another way is to edit your function to get mysqli_result :

public function dbNumRows(mysqli_result $query) 
{
    return $query->num_rows;
}

Note that num_rows is a property of mysqli_result , so this should work as well:

<?php

$query = $Connect->dbExecute("SELECT * FROM oop_usuarios"); 
$num = $query->num_rows;
    
22.05.2015 / 12:47
0

It looks like you are passing the query already done to the class method. This is incorrect. You are mixing procedural calling with object-oriented.

Once the query done you should only use it to extract the data, and not move to another constructor. Even in an inherited class.

The dbNumRows function is not required and can be removed,

$cn = Connect();
$query = $cn->dbExecute('SELECT * from table'); // A query está pronta
$nr = $query->num_rows; // Lendo número de linhas da query

Ref: link

Note that even dbExecute is not required, but I've left it in mind that you can do something other than the default execution of query .

If you are going to use prepare, there is a bit more code, but keep the same process of not creating the same object, create one and use its data inputs and outputs.

Edition 2015-05-24: Example of descending class of mysqli class:

Install XAMPP (tested with xampp-win32-5.6.8-0-VC11-installer.exe); Create a PHP file and copy the following content to it:

<?php
/*
 * Estende a classe mysqli com uma função para fazer uma query a uma tabela
 * específica.
 */
class MinhaConexao extends mysqli {
    function MinhaQuery($filtro) {
        $sql = 'SELECT * FROM cds';
        if ($filtro != '') {
            $sql = $sql . ' WHERE ' . $filtro;
        }
        return parent::query($sql);
    }
}

$cn = new MinhaConexao('localhost', 'root', '', 'cdcol');

if (isset($_POST['from'])) {
    $from = intval($_POST['from']);
} else {
    $from = 1990;
}

if (isset($_POST['to'])) {
    $to = intval($_POST['to']);
} else {
    $to = 2005;
}
?><html>
    <head>
        <title>Teste com classe herdada do MySQLi</title>
    </head>
    <body>
        <form method="post" action="<?php print($_SERVER['SCRIPT_NAME']); ?>" enctype="multipart/form-data">
            Ano de <input type="text" name="from" value="<?php echo $from; ?>"/> a <input type="text" name="to" value="<?php echo $to; ?>"/>
            <input type="submit" />
        </form>
        <?php
        $query = $cn->MinhaQuery('(jahr >= ' . $from . ') AND (jahr <= ' . $to . ')');
        ?>
        Quantidade: <?php print_r($query->num_rows); ?>
        <?php
        while ($r = $query->fetch_assoc()) {
            print('<p>');
            print_r($r);
            print('</p>');
        }
        $query->close();
        ?>
    </body>
</html><?php
$cn->close();
?>

Open the XAMPP control panel, activate the Apache and MySQL servers, and access the file created with the browser.

    
22.05.2015 / 18:38