J. Doe J. Doe - 1 month ago 10
JSON Question

php - sql select "where" equals to a couple of variables?

I am parsing 3 variables from JSON link, each of variable consist of the name of a cinema theater:

$cinema1
$cinema2
$cinema3


After, I need to select an ids of all 3 cinemas from the table, then using that ids I need to look for the table "movietimings" to see if there is movies are playing today, if there is a movie playing in that cinemas today, then I need to go for "movie" table and show all information about that movie.

So, this is what have been accomplished:

$sql2 = "SELECT DISTINCT * FROM cinema WHERE cinemaname = '$cinema1' AND cinemaname='$cinema2' AND cinemaname='$cinema3' ";

$result2 = $conn->query($sql2);

if ($result2->num_rows > 0) {
// output data of each row
while($row2 = $result2->fetch_assoc()) {
$cinemaid2 = $row2['id'];// I have received a cinema id




//getting the main id, since the id I just got is a branch of the main cinema, so the main id that is associated with movie timings is down below
$sql5 = "SELECT DISTINCT * FROM cinemas WHERE cinemaname = '$cinemaid2'";

$result5 = $conn->query($sql5);

if ($result5->num_rows > 0) {
// output data of each row
while($row5 = $result5->fetch_assoc()) {

$cinemaid = $row5['id'];//received main id required

$today = date("m/d/Y");//getting todays date

//now trying to find if a movie today playing
$sql3 = "SELECT * FROM moviecinemashowsassociation WHERE cinemaid LIKE '$cinemaid' AND showdate LIKE '$today'";

$result3 = $conn->query($sql3);

if ($result3->num_rows > 0) {
// output data of each row
while($row3 = $result3->fetch_assoc()) {
$movieid = $row3['movieid'];// selected a movie id played today

//selecting information about movie
$sql4 = "SELECT * FROM movie WHERE id='$movieid'";

$result4 = $conn->query($sql4);

if ($result4->num_rows > 0) {
// output data of each row
while($row4 = $result4->fetch_assoc()) {


The problem is that I have 3 different cinemas which should go through one loop.

Answer

You are looking for a OR condition instead of AND condition since the cinemaname can't hold multiple value at same point of time and thus it would be either of the values specified

WHERE cinemaname = '$cinema1' OR cinemaname = '$cinema2' OR cinemaname = '$cinema3'

(OR) Use a IN operator

WHERE cinemaname IN ('$cinema1' ,'$cinema2' , '$cinema3')
Comments