Error in Function connection class code

Asked

Viewed 502 times

0

I’m in trouble, look at 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>';
        }
    }
 
}

From the following error:

Notice: Undefined Property: bank::$connectionin C: htdocs Netbeansprojects flptrevisan classes conexao.class.php on line 20

Fatal error: Call to a Member Function query() on a non-object in C: htdocs Netbeansprojects flptrevisan classes conexao.class.php on line 20

Line 20 is:

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

1 answer

1

Properties must be declared in the body of the class in the case $conexao was defined as a local variable of the constructor so soon after its call it lost its value.

Define $conexao as a member of the class so:

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

Follow the guidelines of previous question

Just reinforcing a few things:

  • Avoid receiving/manipulating super globals such as $_GET, $_POST, $_COOKIE, $_SESSION directly in methods if you need to pass them as a parameter or reassign the value as a return (in the case outside the class/method).

  • Methods should do/solve only one part of the job/problem, your constructor is doing two fines besides initializing the variables is checking if the server is online, if you really need it 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);
    }

for:

public function servidorOnLine(){
  return $this->conexao->ping();
}
  • Hello, I made the change but still giving error: Fatal error: Call to a Member Function query() on a non-object in C: htdocs Netbeansprojects flptrevisan classes connected to.class.php on line 13 Line 13: $this->connected->query("SELECT * FROM usuarios WHERE email = '$email' AND password = '$password'"); New complete code: http://pastebin.com/uJjR2WEK

  • Remember to do the assignment correctly, here you are adding to a local variable $conexao = new mysqli(...) change to: $this->conexao = new mysqli(...)

  • Deu, went through that error but now comes this: Notice: Undefined variable: sql in C: htdocs Netbeansprojects flptrevisan classes conexao.class.php on line 14 Notice: Trying to get Property of non-Object in C: htdocs Netbeansprojects flptrevisan classes connected.class.php on line 14 The user or password entered is invalid(a)

  • The return of the consultation($this->conexao->query()) needs to be assigned to a variable in the case $sql.

  • Thanks now this working out

  • Now I’m adjusting the code in the structure I want and again gave error. Look, are two files one with each class. , the class of the connection and the class of the Autenticacao, I want to inherit in the Autenticacao the Construct of the opening of the connection. File of the Autenticacao class: http://pastebin.com/a7mfxSjU File of the connected class: http://pastebin.com/FmfCWDRC The error is: Fatal error: Call to a Member Function query() on a non-object in C:htdocs Netbeansprojects flptrevisan classes Autenticacao.class.php on line 8 Always on that line that calls the connection

  • I got the private out of the public connection!

Show 2 more comments

Browser other questions tagged

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