Error fetching database data

Asked

Viewed 93 times

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
            }
        }

        }

2 answers

2

You are returning the variable $result as string. In your Cód is 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 it 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;
  • it gives the same error if I give an echo in the $search it returns 1, but I can’t pass the data to the other page.

1

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

Soon you must 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'];
}
  • 1

    understood, but added the while can not print anything on the screen

  • 1

    @I’m sorry, change inside the while, echo $result for echo $linha or echo $linha['nome'] etc..

  • 1

    In fact the variable $linha is a array then to print all the data contained in it must use print_r($linha)

  • 1

    continues without returning anything, already has a mysqli_fetch_assoc within Else will be why?

  • 1

    Possibly .

  • 1

    I need the result of this query to log in and pass the user data to the other page (logged in.php) Is there an easier way to search this data, log in and still pass to the other page? wanted to pass all at once to not do another query on the other page.

  • 1

    You’re doing it already, and saving in one sessão, if the data is correct, then the data is being saved in the sessão, to redeem the data on the page logado.php of a echo $_SESSION['nome']

  • 1

    got it, pulled the other fetch_assoc from above, thank you!

Show 3 more comments

Browser other questions tagged

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