Error while performing a SQL filter

Asked

Viewed 44 times

0

I cannot identify the error that is happening in the code below:

$bannerguia=mysqli_query($con,"SELECT id, senha FROM conta WHERE senha = '$senhalog'");
while($painel_banner=mysqli_fetch_array($bannerguia))
{
     $issenha = $painel_banner['senha'];
     $isid = $painel_banner['id'];
}

In SQL when I put the variable $senhalog in quotes '$senhalog' does not work filter, if I do not quote works, but gives error if not locate anything.

In all the systems I develop I put in quotes and everything works perfectly, I do not understand what is happening here.

  • 1

    Give an echo in the query to see how the return is coming.

  • Check whether the variable $senhalog is at the correct value, if you are testing sql directly in the terminal or phpmyadmin or Workbench or something, the error may be in sql

2 answers

1


Think with me, you said "if you can’t find anything without quotation marks, it’s a mistake", If I understood the no locates anything would be $senhalog? If yes will definitely give error because the generated select will be:

SELECT id, senha FROM conta WHERE senha =

and this gives error in any bank.

If the password is string type you will need quotes if it is not do a if ternario in the variable saying that if it has value it is it otherwise puts something like null(it makes no sense that $senhalog does not exist).

1

Put your queries in variables this way you will have more control of the result.

<?php
    $sql = "SELECT id, senha FROM conta WHERE senha ='".$senhalog."';";
    //echo $sql;
    $bannerguia=mysqli_query($con,$sql);
    while($painel_banner=mysqli_fetch_array($bannerguia))
    {
         $issenha = $painel_banner['senha'];
         $isid = $painel_banner['id'];
    }
?>

Browser other questions tagged

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