3
I have a question about some function of Mysqli
In the conventional way we would do so:
public function Numrows($sql) {
return mysqli_num_rows($sql);
}
And how 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 of the 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.
I’ve tried to use as you recommended but got back two errors see:
Warning: mysqli::prepare() expects parameter 1 to be string, object given in C:\wamp\www\oop\modules\class\Connect.class.php on line 35
the other mistake:Fatal error: Call to a member function num_rows() on a non-object in C:\wamp\www\oop\modules\class\Connect.class.php on line 35
this is my methodpublic function dbNumRows($Query) {
 return $this->prepare($Query)->num_rows();
 }
– Guilherme Alves
@Guilhermealves you are passing an incorrect argument. The method
prepare
expects a string, and you’re passing something else.– gmsantos
I am passing a variable that is the parameter I am passing here see:
$query = $Connect->dbExecute("SELECT * FROM oop_usuarios");
$num= $Connect->dbNumRows($query );
I’m at my job now when I get home I see what I can do, if you have any tips...– Guilherme Alves
@Guilhermealves this way is wrong. Post your complete connection class in the question.
– gmsantos
was added in the topic.
– Guilherme Alves
@Guilhermealves Tente
$db->dbNumRows('SELECT * FROM oop_usuarios');
. Pass only the querySELECT * FROM oop_usuarios
– gmsantos
Let’s go continue this discussion in chat.
– Guilherme Alves
Yes this way right, but I wanted to take the parameter passed!
– Guilherme Alves