Error fetching data from bank

2

When I try to do a BD search for a login, it returns the following error:

  

Recoverable fatal error: Object of class mysqli_result could not be   converted to string in C: \ xampp \ htdocs \ TCC \ index.php on line 66

I need to pass all these result values to the other page.

if (isset($_POST['entrar']) && $_POST['entrar'] == "login"){
    $email = $_POST['email'];
    $senha = $_POST['senha'];

        if(empty($email) || empty($senha)){
            ?>
            <script type="text/javascript"> alert ('preencha todos os campos');
            </script>
            <?php
        }else{
                $query = "SELECT id,nome, adm, email, senha, rua_num, bairro, tel FROM usuarios WHERE email = '$email' AND senha = '$senha' ";
                $result = mysqli_query($conn, $query); 
                $busca = mysqli_num_rows($result);
                $linha = mysqli_fetch_assoc($result);


            if($busca > 0){
                $_SESSION['id'] = $linha['id'];
                $_SESSION['nome'] = $linha['nome'];
                $_SESSION['rua_num'] = $linha['rua_num'];
                $_SESSION['bairro'] = $linha['bairro'];
                $_SESSION['email'] = $linha['email'];
                $_SESSION['tel'] = $linha['tel'];
                $_SESSION['senha'] = $linha['senha'];
            echo "$result";
                //header('location: logado.php');
                //echo "$email - $senha - $nome";



                //exit;
            }else{
                ?>
                <script type="text/javascript"> alert('usuario ou senha incorretos!');</script>
                <?php
            }
        }

        }
    
asked by anonymous 03.09.2017 / 00:15

2 answers

2

You are returning the variable $result as a string. In your code like this:

 if($busca > 0){
            $_SESSION['id'] = $linha['id'];
            $_SESSION['nome'] = $linha['nome'];
            $_SESSION['rua_num'] = $linha['rua_num'];
            $_SESSION['bairro'] = $linha['bairro'];
            $_SESSION['email'] = $linha['email'];
            $_SESSION['tel'] = $linha['tel'];
            $_SESSION['senha'] = $linha['senha'];
        echo "$result"; <-- o erro tá aqui

You have to do this:

 if($busca > 0){
            $_SESSION['id'] = $linha['id'];
            $_SESSION['nome'] = $linha['nome'];
            $_SESSION['rua_num'] = $linha['rua_num'];
            $_SESSION['bairro'] = $linha['bairro'];
            $_SESSION['email'] = $linha['email'];
            $_SESSION['tel'] = $linha['tel'];
            $_SESSION['senha'] = $linha['senha'];
        echo $result;
    
03.09.2017 / 01:19
1

mysqli_query() returns an object resource for the variable $result , not a string .

You should make a loop

while ($linha = $result->fetch_assoc()) {
    $_SESSION['id'] = $linha['id'];
    $_SESSION['nome'] = $linha['nome'];
    $_SESSION['rua_num'] = $linha['rua_num'];
    $_SESSION['bairro'] = $linha['bairro'];
    $_SESSION['email'] = $linha['email'];
    $_SESSION['tel'] = $linha['tel'];
    $_SESSION['senha'] = $linha['senha'];
}
    
03.09.2017 / 01:27