0

I have the following code

$sql="select * from movieTrivia WHERE id IN ('9,2,1,4,7')";
$array = array();
if ($result = $mysqli->query($sql)) {
    // If the query was sucsessfull, we can get the rows
    while ($row= $result->fetch_array()) {
    $array[] = $row;

}
} else {
    // If the query failed, do something here
}
   $a               = $row['title']; 
    $b                  = $row[1]['title'];  //out put second id in array (2)?
    $c                  = $row['title']; 
    $d                  = $row['title']; 
echo json_encode(array("a" => $a, "b" => $b, "c" => $c, "d" => $d, "answerImg" => $answerImg));

Out Put is: $a = title1 $b = null $c = title1 $d = title1

It only pulls the title from the first ID in the IN array (9). How do i pull the title of 2, 1, 4 and 7

3
  • 1
    try with $sql="select * from movieTrivia WHERE id IN (9,2,1,4,7)"; Commented Nov 16, 2015 at 20:04
  • Your fetch is not returning a multidimensional array. $a, ,$c, and $d will all have the same values. You overwrite on every iteration. Commented Nov 16, 2015 at 20:06
  • OKay, as i suspected, how do i put the data from (9,2,1,4,7) into an array I can access as i can above? Commented Nov 17, 2015 at 0:03

1 Answer 1

1

you dont need quotes inside the IN brackets

$sql="select * from movieTrivia WHERE `id` IN (9,2,1,4,7)";
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, i made the change

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.