Mysqli query returns FALSE instead of returning database records

Asked

Viewed 25 times

0

Hello, I’m trying to query the user login data in the php database but I noticed that the variable that receives the function that makes the query always returns empty and I var_dump this variable that showed me only the FALSE value(Which indicates there was an error in the query, right?). Below the code for better understanding.

Error:

Warning: mysqli_fetch_assoc() expects Parameter 1 to be mysqli_result, Boolean Given in /opt/lampp/htdocs/projects/centralgames/dao/usuario_dao.php on line 35 NULL

'valida_login.php' is called by login form and receives user data via 'POST'.

<?php

session_start();

//Recebe os campos da página login
$input_usuario = $_POST['usuario'];
$input_senha = $_POST['senha'];

include ('../dao/usuario_dao.php');

$usuario = seleciona_usuario($input_usuario, $input_senha);

//Se o resultado retornar vazio executa
if(empty($usuario))
{
    //Mensagem de erro
    $_SESSION['login_erro'] = "Usuário e/ou senha inválido(s)! Por favor, tente novamente.";


    var_dump($usuario);
    //Redireciona para a tela de login
    //header('Location: ../index.php');
}
else
{
    //Sessions que recebem os valores dos campos do banco de dados
    $_SESSION['nome_usuario'] = $usuario['nome'];
    $_SESSION['acesso_usuario'] = $usuario['permissao'];
    $_SESSION['login_usuario'] = $usuario['login'];

    $_SESSION['permissao'];

    if($_SESSION['acesso_usuario'] == 1)
    {
        $_SESSION['permissao'] = "Master";
        header('Location: menu_master.php');
    }
    else//Implementar outras permissões
    {
        $_SESSION['permissao'] = "Usuário";
        header("Location: menu_usuario.php");
    }
}

'usuario_dao.php' is where the function that makes the query in the database is.

<?php    
include ('../models/conexao.php');

//Função que seleciona usuário
function seleciona_usuario($login, $senha)
{
    $conn = conectar();

    $query = "SELECT * FROM tb_usuario WHERE (login='$login' AND senha=MD5('$senha'))";

    $result = mysqli_query($conn, $query);

    return mysqli_fetch_assoc($result);
}

Thank you in advance to those who can help.

  • in the user file_dao.php add below $result = ... the code to follow if($result === FALSE) {&#xA; die(mysqli_error());&#xA;} and put us to see the cause

  • Ok, I put the code and apparently the code itself generated an error that says the function 'mysqli_error()' expects to receive a parameter. Warning: mysqli_error() expects Exactly 1 Parameter, 0 Given in /opt/lampp/htdocs/projects/centralgames/dao/usuario_dao.php on line 35

  • What a thing! then put $result within the function mysqli_error and rotate to see

  • 1

    Thank you for returning. Searching I saw that the function expected to receive the connection as a parameter and I was able to print the error that makes you ashamed to speak. hahaha I was able to run the query in Phpmyadmin and in the code not simply because I was connected to a different database and the error returned to me that the table I was trying to access in the code did not exist. I apologize for any inconvenience and thank everyone who helped. Do what? Living and learning. Thank you.

No answers

Browser other questions tagged

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