Doubt with the LIKE query

Asked

Viewed 50 times

0

I developed a function that searches the bank for a client by ID:

function buscaId ($id,$conexao){
    $resultados= array();
    $query= "select * from cliente where id like '$id%' ";
    $resultado= mysqli_query($conexao,$query);
    while ($resultado_cliente = mysqli_fetch_assoc($resultado)) {
            array_push($resultados, $resultado_cliente);



        }

            return $resultados;
    }

But when I type "%%" ou "VAZIO " in "Search", it brings all customers from the bank.

How do I disable this? The bank will have 7,000 lines.

  • creates a condition ( IF ) in the function if $id for empty already returns ( return ) and does not perform the rest;

  • if it is by ID why search with like?

  • Thanks arllondias has no need of the LIKE I took and solved kkk

1 answer

0

You can use php’s PDO. It’s safer and has a higher abstraction. What’s more, it doesn’t need the cycle because the query only returns a record. Take an example.

    function buscaId ($conexao, $id)
    {
        $resultados= array();
        $query= "select * from users where id = ?";        
        $stmt = $conexao->prepare($query);
        $stmt->bindParam(1, $id, PDO::PARAM_INT);
        $stmt->execute();

        $rows = $stmt->fetch();

        return $rows;
    }

   $resultado = buscaId($conexao, 1);
   echo 'Id: '.$resultado['id'].' - Nome:' .$resultado['name'] ;
  • Thanks for the tip and the example Mia I will try to apply the PDO

Browser other questions tagged

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