Search in mysqli with a field separated by comma

0

I would like to do a search on mysql using a variable that contains data separated by,. So far I have always done this with a loop, but the same is some way of looking directly using that variable.

The idea would be to find all the ids stored in the database that are in that list.

I have absolutely no code to put a part.

example of data structure:

id = int autoincrement
concepto = varchar
precio = int

and let's call variable $idaconsultar and content has "1,5,8,14"

The search I would like to do is: consult the data that matches the id in the variable $idaconsultar , without having to do a foreach .

    
asked by Killpe 22.11.2017 в 12:10
source

1 answer

1

You could try something like:

    $stmt = $mysqli->prepare("SELECT id,concepto,precio FROM TuTabla where id in (?)"); 
    $stmt->bind_param("s", $idaconsultar);

This would create a query to which you assign as a parameter that String where the ids that you want to search appear and the query would return the records in which those ids were.

Version without parameters:

SELECT id,concepto,precio FROM TuTabla where id in ("1,5,8,14");
    
answered by 22.11.2017 / 13:29
source