Is there a better way to filter results with PHP / MYSQL?

0

Basically I'm starting with PHP by doing a small web search engine with a database that has some products.

My doubt comes, good I want to get the user to put any word I throw all the results that are related to what was entered , I did it this way:

$registros = mysqli_query($conexion, "SELECT * FROM productos WHERE 
                                          idProducto LIKE '%$keywords%' OR
                                          Nombre LIKE '%$keywords%' OR 
                                          Marca LIKE '%$keywords%' ");

I do not know if there is any more efficient way to achieve it, I mean that if it is necessary to put it resembles what was entered with each data of my DB. Search the internet but I can not find a clear answer that will remove the doubt.

<table> 
            <tr>
                <th>ID del producto</th>
                <th>Nombre</th>
                <th>Marca</th>
            </tr>

if( isset( $_GET["keywords"] ) ){
    $keywords = htmlspecialchars(strip_tags(trim($_GET["keywords"])));

} 
if( empty($_GET["keywords"]) ){
    echo "<p>Tenes que ingresar un criterio de busqueda</p>";
} else{
    $conexion = mysqli_connect("localhost", "root", "", "busqueda") or die("Could not connect");
    mysqli_select_db($conexion, "busqueda") or die("Todo mal");
    $registros = mysqli_query($conexion, "SELECT * FROM productos WHERE 
                                          idProducto LIKE '%$keywords%' OR
                                          Nombre LIKE '%$keywords%' OR 
                                          Marca LIKE '%$keywords%' ");
    $cantidad = mysqli_num_rows($registros); 
    echo "<p>Se encontraron $cantidad resultado con la palabra $keywords</p>";
    while( $dato = mysqli_fetch_assoc($registros) ){
?>  
            <tr>
                <th><?php echo $dato["idProducto"]; ?></th>
                <th><?php echo $dato["Nombre"]; ?></th>
                <th><?php echo $dato["Marca"]; ?></th>
            </tr>

<?php
    }
}
?> 

    
asked by Eric 19.09.2018 в 15:03
source

1 answer

-1

You can create a search engine with PHP, MySQL and Ajax, to make filters or efficient searches.

Example: link

Or if you want you can use DataTable or FooTable with Bootstrap or any other CSS framework, which help a lot and are easy to implement (from behind they use JS, AJAX, among others), you just have to read the documentation well.

    
answered by 20.09.2018 в 05:53