Notice: Undefined variable: connection

Asked

Viewed 593 times

-1

Recently I changed all my code mysql for mysqli. It just gave me a lot of headaches.
He gives me the following mistakes:

Notice: Undefined variable: conexao in D:\Programas\wamp64\www\admin\css\header.php on line 44
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in D:\Programas\wamp64\www\admin\css\header.php on line 44
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in D:\Programas\wamp64\www\admin\css\header.php on line 45

Notice: Undefined variable: conexao in D:\Programas\wamp64\www\admin\css\Cadastro.class.php on line 15
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in D:\Programas\wamp64\www\admin\css\Cadastro.class.php on line 15

Couldn’t insert when to insert.

Currently my codes are in:

index php.
php.
css/header.php <- is linked
css/DB.class.php
css/Cadastro.class.php <- is linked

DB.class.php:

<?php
class DB{
    public function conectar(){
        $conexao=mysqli_connect("localhost","root","","painel")or die("Não foi possível conectar na database.");
    return $conexao;
    }
}
?>


Where did I go wrong?

Thanks Fernando, the mistakes have decreased. Now is giving the following mistakes:

Notice: Undefined variable: conectar in D:\Programas\wamp64\www\admin\css\Cadastro.class.php on line 15

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in D:\Programas\wamp64\www\admin\css\Cadastro.class.php on line 15

I changed from $conexao for $conectar in the archives header.php and Cadastro.class.php. 2 errors left, only 2 more.

  • Where and how it’s calling is variable?

  • How’s the header.php?

  • It is linked in the topic @Jonathandetoni e Francisco

  • Put the relevant parts of the codes here in the question, so that everyone can have access

1 answer

0


When you must have confused the connection variable.

You are using this on line 17 and 18 of your header.php:

$conectar=new DB;
$conectar=$conectar->conectar();

And so in the line of error:

$validaremail=mysqli_query($conexao, "SELECT * FROM usuarios WHERE email='$email'");

Try changing the variable $connection for $connect, as you started there.

should look like this:

$validaremail=mysqli_query($conectar, "SELECT * FROM usuarios WHERE email='$email'");

Your DB class function connect() returning return $conexao;, but what counts is the variable that calls this function in the code:

$conectar=$conectar->conectar();

If even correcting this error, new errors appear, you edit this question by putting the new errors that appear at the end.

EDITED

Class error Register of the archive Cadastro.class.php:

Classes and functions cannot access external variables. So line 15 ($insert=mysqli_query($conexao, "INSERT...) of your class cannot access the variable $conexao, and even if you change to $conectar also won’t work.

You need to insert the variable into the class, and suggest the magic method __construct() sort of like this:

<?php

   class Cadastro{
        private $conexao;

        public function __construct($varconexao) {
          $this->conexao = $varconexao;
        }

         public function cadastrar($nome, $nickt, $nickp, $email, $face, $senha, $tel1, $tel2){
 ...
   $insert=mysqli_query($this->conexao, "INSERT INTO usuario...
 ...
         }
    }
 ?>

That is, you create a variable $conexao for your class, and when starting the class you assign its external variable to within your class using: new Cadastro($sua_variavel_de_conexao);.

So now just fix your header.php on the line you call the function:

     $cadastro = new Cadastro($conectar);
     echo "<div class='flash'>";
     $cadastro->cadastrar($nome, $nickt, $nickp, $email, $face, $senha, $tel1, $tel2);
     echo "</div>";

You already use the variable $connect for your connection class, I recommend using another variable for the registration, as I did above with $register. If you use the same variable as you are currently doing $conectar=new Cadastro; or $conectar=$conectar->cadastrar, you end up overwriting it, and in the future it may give you more headache, depending on how you create your code.

  • Managed to solve 2 errors, but there were two more. I explained in the topic.

  • I edited the answer by putting the solution to the 2 errors that are occurring. Give a little glance resolves, and if it works out don’t forget after marking the answer as correct to help other people who have the same problem as you in the future. ^^

  • I think I did something wrong. Now it gives the following errors: https://hastebin.com/ovajizupol.tex This error did not understand, because as I can see above, I made the include of Cadastro.class. Currently the header looks like this: https://hastebin.com/howosifugo.xml and Cadastro.class: https://hastebin.com/vobijepewe.xml

  • When you start a class you assign the class to a variable, in your case you did $cadastro=new Cadastro($conectar);, Then the register variable will have the same functions as your class. So when you call the function you should do so: $variaveldaclasse->funcao(). Your mistake is that where you should call the function you put a $ calling another non-existent variable $cadastro->$cadastrar($nome, $ni.... This way is wrong. you should call so: $cadastro->cadastrar($nome, $ni....

Browser other questions tagged

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