0
Good afternoon, everyone
I’m having an error: Warning: Notice: Undefined variable: mysqli in /var/www/html/crudgenerico/classes/Conexao.class.php on line 59
Warning: mysqli_query() expects Parameter 1 to be mysqli, null Given in /var/www/html/crudgenerico/classes/Conexao.class.php on line 59
Warning: mysqli_error() expects Exactly 1 Parameter, 0 Given in /var/www/html/crudgenerico/classes/Conexao.class.php on line 59
public $host = "localhost";
public $user = "root";
public $password = "xxxxxxxx";
public $db = "crud";
public $dataset = NULL;
public $linhasAfetadas = -1;
public function __construct() {
$this->conecta();
}
public function __destruct() {
}
public function conecta() {
$mysqli = new mysqli($this->host, $this->user, $this->password, $this->db);
if ($mysqli->connect_error) {
die('Erro de conexão (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}
}
public function inserir($objeto) {
$sql = "INSERT INTO " . $objeto->tabela . " (";
for($i = 0; $i < count($objeto->campos_valores); $i++) {
$sql .= key($objeto->campos_valores);
if($i < (count($objeto->campos_valores)-1)) {
$sql .= ", ";
} else {
$sql .= ") ";
}
next($objeto->campos_valores);
}
reset($objeto->campos_valores);
$sql .= "VALUES (";
for($i = 0; $i < count($objeto->campos_valores); $i++) {
$sql .= is_numeric($objeto->campos_valores[key($objeto->campos_valores)]) ?
$objeto->campos_valores[key($objeto->campos_valores)] :
"'" . $objeto->campos_valores[key($objeto->campos_valores)] . "'";
if($i < (count($objeto->campos_valores)-1)) {
$sql .= ", ";
} else {
$sql .= ") ";
}
next($objeto->campos_valores);
}
echo $sql;
return $this->executaSql($sql);
}
public function executaSql($sql = NULL) {
$query = mysqli_query($mysqli, $sql) or die(mysqli_error());
}
}
Missed passing the connection to the
mysqli_query()
he is always the first argument.– rray
The syntax is
$query = mysqli_query($conexao,$sql)
, more details in the linked post in the closing.– Bacco
I did so and it gave the following error: Notice: Undefined variable: conexao in /var/www/html/crudgenerico/classes/Conexao.class.php on line 59 Warning: mysqli_query() expects Parameter 1 to be mysqli, null Given in /var/www/html/crudgenerico/classes/Conexao.class.php on line 59 Warning: mysqli_error() expects Exactly 1 Parameter, 0 Given in /var/www/html/crudgenerico/classes/Conexao.class.php on line 59
– Marcelo
You probably didn’t pass the variable to your method, right? The site is a cool way to take away programming doubts, but is no longer as suitable as "remote Debugger". Basically the error says $mysql is null, that is, you did not pass this parameter in your function (and did not use a class member instead).
– Bacco
Okay, I’ll try to see where the mistake is.
– Marcelo
The problem is
$mysqli
(the connection) in the methodconecta()
, she is a local variable or whenconcta()
finish excutar it will no longer exist. The solution I recommend is to make$mysqli
a class member (like $host, $user etc ...) and function callsmysqli_
do as follows:mysqli_query($this->mysqli, $sql)
– rray
Marcelo @rray’s solution is well suited, and should be simple to implement, but if you have difficulty, leave a comment here. Basically you will use for example a
public $mysqli
at the beginning of the code, and in place of the$mysqli
existing, exchanged by the member$this->mysqli
.– Bacco
PS: ta missing connection here tb:
or die( mysqli_error($this->mysqli) );
– Bacco