Why is my query not working with the php variable

Asked

Viewed 340 times

0

I’m in trouble with my query of mysql. The value is passed from the php form correctly with the $_POST, but when I add the variable to compare the query does not send a response, but if I add the exact value that is in the database I get a response, but I need to use the value that comes from the variable.

that way it works

//pegar matricula
  $query_matricula = mysqli_query($conexao,"SELECT matricula FROM usuario where nome = 'Maria'") or die("ERROR" .mysqli_error());
  $row = mysqli_fetch_assoc($query_matricula);
  $numero_matricula = $row['matricula'];
  echo $row['matricula'];

that way it doesn’t work

 //pegar matricula
  $query_matricula = mysqli_query($conexao,"SELECT matricula FROM usuario where nome = '$nome'") or die("ERROR" .mysqli_error());
  $row = mysqli_fetch_assoc($query_matricula);
  $numero_matricula = $row['matricula'];
  echo $row['matricula'];
  • You can have space there. Recommended => http://answall.com/q/93028/91

  • even if the name does not have spaces or accents, it seems that the fact that it is coming from another query to the bank is causing the select not to accept as equal

  • After the myqli_query() place, echo 'teste';, see if anything pops up on the screen.

  • When I add the value manually echo sends me the right answer, but when I put the variable it does not send anything, as if I did not find the same value

  • The 'test' appeared on the screen?

  • I put echo 'test'; as he spoke and appeared: -test

  • Do not tell where exactly is the error, try to print the query. try: $sql = "SELECT matricula FROM usuario where nome = '$nome'";
echo $sql;
$query_matricula = mysqli_query($conexao, $sql) or die("ERROR" .mysqli_error($conexao));

  • 1

    I think it was the whitespace problem. I added a function to erase the spaces and he sent me the correct answer. Thank you for your attention

Show 3 more comments

2 answers

2


Make sure that what comes from the post is equal to the content searched. I like to do so... echo '-'.$nome.'-';. You may have spaces in your name. Clear them: $nome = trim( $_POST[ 'nome' ] );.

  • I added the function to erase the spaces and solved it. Thank you.

  • 1

    It was a pleasure to help you

-1

try this way

$query_matricula = mysqli_query($conexao,"SELECT matricula FROM usuario where nome = '{$nome}'") or die("ERROR" .mysqli_error());

Browser other questions tagged

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