Filter search does not work

Asked

Viewed 214 times

1

I have a form that passes the query data and an external PHP file called busca.php

My problem is that I don’t get any php error to know the problem of it not finding the filter search. When I use the normal search it returns me all the values of the database.

What’s going on? Follow the code of the program, where q is to receive the text field consulta file consulta.php:

if(isset($_POST['buscar'])){
    $q = $con->real_escape_string($_POST['consulta']);
    $query_Busca = "SELECT * FROM equipamento WHERE tombamento LIKE '%$q%'";
    $Busca = mysqli_query($con,$query_Busca) or die($con->error);
    // Check results
    if($Busca){
        //Se for um sucesso!
        $row_Busca = mysqli_fetch_assoc($Busca);
        $totalRows_Busca = $Busca->num_rows;
    }

In this else the function is working normal giving all the database data as wish the problem is even in the code above where I want to find the equipment only by the tipping number that is in "query" however it does not think and returns me error.

} else {
    $query = "SELECT * FROM equipamento";
    $Busca = $con->query($query) or die($con->error);
    $row_Busca = mysqli_fetch_assoc($Busca);
    $totalRows_Busca = $Busca->num_rows;
}
  • See if $_POST['busca] has some value and check if the query returns something, can give: print_r($row_Busca);

  • I put and I return nothing @lost, there appeared nothing, this right?

  • If I put this way in the if $row_Busca = $Busca->fetch_assoc($Busca); works, but he gives me that mistake: mysqli_result::fetch_assoc() expects exactly 0 parameters, 1

  • I don’t know if it helps, but tombamento is type INT ie, only number, this right the LIKE I put? What makes me curious is that no error.

  • Like does not work with int fields.

  • what I use to compare then?

Show 1 more comment

1 answer

1

try to standardize your code, because in one part you use mysqli_ functions and in another use objects, and these details end up causing headache and difficulties in finding the problems.

copying from the working block would look like this:

(Obs.: if the tipping field is int, use (int) to convert to integer instead of like.)

if(isset($_POST['buscar'])){
    $q = $con->real_escape_string($_POST['consulta']);
    $query = "SELECT * FROM equipamento WHERE tombamento = '" . (int)$q . "'";
    $Busca = $con->query($query) or die($con->error);
    // Check results
    if($Busca){
        //Se for um sucesso!
        $row_Busca = mysqli_fetch_assoc($Busca);
        $totalRows_Busca = $Busca->num_rows;
    }
  • but I was in doubt about these variables $_POST['buscar'] and $_POST['consulta'] because you condition with the 'search' and use the 'query'?

  • Search is a button, the query is the field with the number. I tested and it didn’t work, I want to leave everything with Mysqli, if you know how I arrange to leave everything Mysqli thank you too.

  • "didn’t work" is too vague, can you determine what he’s doing? add a echo $query; just below the $query = "SELECT ... and see if it will print the query correctly with the number you typed, then copy the query and test it in phpmyadmin... ie if php is not giving you the error messages, look for ways to debug it for yourself.

  • he printed me: SELECT * FROM equipamento WHERE tombamento LIKE '%458791%'

  • you are still using the like in the INT field, see in the above code that I modify this part...

  • I modified this and it does not echo me in the result. I printed your select all and no errors. SELECT * FROM equipamento WHERE tombamento = '(int)458791' that number was the value I entered to find in Mysql.

  • I tried to make a direct modification and found this message now, what can it be? SELECT * FROM equipamento WHERE tombamento = 458791Unknown column 'tipping' in 'Where clause'

  • The worst thing now is that I solved this by putting it like this: $row_Busca = mysqli_fetch_assoc($query_Busca); It finds but returns me with PHP error: Warning: mysqli_fetch_assoc() expects Parameter 1 to be mysqli_result, string Given

  • There is no column called tipping on your equipment table... Check.

  • I got it, I was gonna take that part of the code: $row_Busca = mysqli_fetch_assoc($Busca); that worked! Thank you all for your help.

Show 5 more comments

Browser other questions tagged

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