How do I use the data returned by a SQL WHERE IN(x,y)
statement with PHP?
$sqlquery = "SELECT * FROM schedules WHERE userid IN ('35','101','45')";
if ($test_stmt=$mysqli->prepare($sqlquery)) { //Get all data in schedule row
$test_stmt->execute(); //Execute the prepared query
$test_stmt->store_result(); //Store result
$test_stmt->fetch();
do something...
}
else {
return null;
}
}
The SQL query returns the correct three table rows, I'm just not sure how to access them with PHP.
How do I create an array to store the values the query returns? Or is there a better way to store this data to later be manipulated?
Thanks
The most common way to do this is through a loop akin to the following:
$results=array();
while($row=$test_stmt->fetch())
{
$results[]=$row;
}
This inserts the row into the results variable, which is an array. At the end of the loop, the array will contain a copy of everything the query returned.
You can also pick and choose what you do with particular columns in the results like this:
$results=array();
while($row=$test_stmt->fetch())
{
$results[]['ID']=$row['ID'];
}
This assumes there is a column called "ID" in the results though.