0

I'm doing a webapp with PHP and I have an array with some values which is greatherThan[], I need to make a prepared statement for each value of the array, for example if the array has the values 4,5,6

The php code is:

$stmt11 = $mysqli->prepare("SELECT nombre FROM usuarios WHERE id = ?");
$stmt11->bind_param('i', $greaterThan[]);
$stmt11->execute();
$stmt11->store_result();
$stmt11->bind_result($nombresmayores);
$arraynombresmayores = array();
$stmt11->store_result();
while($stmt11->fetch()){
    $arraynombresmayores[] = $nombresmayores;
}   
C.Xammar
  • 167
  • 1
  • 1
  • 11
  • Do you need a separate statement for each value in your array, or are you looking to find the users where the ids are 4, 5, or 6? Is the goal to have one statement where you get all the rows where the id matches one of the values? So if you give it 3 id values, you might get up to 3 rows back? – stratedge Apr 13 '17 at 03:40
  • did you want `id IN()` ? id so see: http://stackoverflow.com/questions/10501394/how-to-use-prepared-statements-in-queries-with-an-in-clause-in-php –  Apr 13 '17 at 03:42
  • @stratedge Yes, I want to end up with an array with the "nombre" variable for each id. But I need it in some kind of loop since the array "greaterThan" is dynamic and might have different number of vars each time. – C.Xammar Apr 13 '17 at 03:45
  • then you should be using `id in()` and no loop not one query per id –  Apr 13 '17 at 03:50
  • @nogad is correct - if you want to match the same column to multiple values, use MySQL's `WHERE column_name IN (value1, value2...)` capability. It will return all rows where there is a matching value. See the referenced question to see how its done with prepared statements in PHP. It's a little tricky because you need to dynamically build the SQL statement depending on how many items are in your array. – stratedge Apr 13 '17 at 03:57

0 Answers0