Error in class code connection function

0

I'm having trouble, see the code I made:

<?php
class banco {
   public function __construct() {

        $banco_hostremoto = "127.0.0.1";
        $banco_hostname = "localhost";
        $banco_usuario="root";
        $banco_passwd="";
        $banco_nome="flptrevisan";

        $conn = mysql_connect($banco_hostname,$banco_usuario,$banco_passwd);
        if (!mysql_ping($conn)) {   $FORADOAR = TRUE;   }
        else if (mysql_ping($conn)) {
            $FORADOAR = FALSE;
            $conexao = new mysqli($banco_hostname,$banco_usuario,$banco_passwd,$banco_nome);
        }

    }
    public function signIn($email,$senha) {
        $this->conexao->query("SELECT * FROM usuarios WHERE email = '$email' AND senha = '$senha'");
        if ($sql->num_rows==1){
                while($linha =  $sql->fetch_array()) {
                        $usuario = $linha['nome'];
                        $acessos = $linha['acessos']+1;
                }
                $ip=$_SERVER["REMOTE_ADDR"];
                $sql = $conexao->prepare("UPDATE usuarios SET acessos = ?, ip = ? WHERE email = ?");
                $sql->bind_param('iss',$acessos,$ip,$email);
                $sql->execute();
                $_SESSION["autenticado"]=$email;
                $_SESSION["usuario"]=$usuario;
                header('location:index.php');
        }
        else {
                echo '<div class="alertaIndexMSG">O usuário ou senha inserido é inválido(a)</div>';
        }
    }

}

Give the following error:

  

Notice: Undefined property: bank :: $ connection in   C: \ htdocs \ NetBeansProjects \ flptrevisan \ classes \ connection.class.php on   line 20

     

Fatal error: Call a member function query () on a non-object in   C: \ htdocs \ NetBeansProjects \ flptrevisan \ classes \ connection.class.php on   line 20

Line 20 is:

$ this-> connection-> query ("SELECT * FROM users WHERE email = '$ email' AND password = '$ password'");

    
asked by anonymous 06.08.2014 / 14:59

1 answer

1

Properties must be declared in the body of the class in case $conexao was set as a local constructor variable then shortly after its call it would have lost its value.

Set $conexao as a class member like this:

class banco {
   private $conexao;
//........

Follow the directions in previous question

Just reinforcing some things:

  • Avoid receiving / manipulating global variables like $ _GET, $ _POST, $ _COOKIE, $ _SESSION directly into methods if you need to pass them as a parameter or reassign the value as a return (in the case outside the class / method) / p>

  • Methods should do / resolve only part of the work / problem, your builder is doing two things in addition to initializing the variables is checking if the server is online, if you really need to create a new method:

Change:

public function __construct() {
    $conn = mysql_connect($banco_hostname,$banco_usuario,$banco_passwd);
    if (!mysql_ping($conn)) {
        $FORADOAR = TRUE;
    }else if (mysql_ping($conn)){
        $FORADOAR = FALSE;
        $conexao = new  mysqli($banco_hostname,$banco_usuario,$banco_passwd,$banco_nome);
    }

To:

public function servidorOnLine(){
  return $this->conexao->ping();
}
  • There is a mysql_ * lost there in the constructor, forget it, #
06.08.2014 / 15:09