【问题标题】:php - sql select "where" equals to a couple of variables?php - sql select“where”等于几个变量?
【发布时间】:2016-10-28 18:51:48
【问题描述】:

我正在从 JSON 链接解析 3 个变量,每个变量都包含电影院的名称:

$cinema1
$cinema2
$cinema3

之后,我需要从表中选择所有 3 个电影院的 id,然后使用该 id 查找表“movietimings”以查看今天是否有电影正在播放,是否有电影正在播放今天电影院,然后我需要去“电影”表并显示有关该电影的所有信息。

所以,这就是已经完成的:

    $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()) {

问题是我有 3 个不同的电影院,它们应该经过一个循环。

【问题讨论】:

  • 请为我们定义:“不工作”。变量的值是...? db架构是什么? db 中的值是什么?
  • 你需要学习布尔逻辑。您是在说“电影名称字段必须同时是以下所有三个值”。你想要一个or。 "可以是以下任意值"
  • 感谢您的回复。我正在更改问题,因为我似乎没有清楚地理解问题。
  • Little Bobbyyour script is at risk for SQL Injection Attacks.。即使escaping the string 也不安全! SQL 注入!不再只是早餐!
  • 我已经更新了问题,任何帮助将不胜感激。

标签: php mysql sql json


【解决方案1】:

您正在寻找 OR 条件而不是 AND 条件,因为 cinemaname 不能在同一时间点保存多个值,因此它将是指定的值中的任何一个

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

(或)使用IN 运算符

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

【讨论】:

  • 感谢您的回答,我需要从所有 3 家电影院获取电影时间。我已经更新了这个问题,如果你能看一下,将不胜感激。
  • @J.Doe,不确定您要做什么,但此条件 WHERE cinemaname = '$cinema1' AND cinemaname='$cinema2' AND cinemaname='$cinema3' 将始终为 false 导致您的查询返回 0 条记录。改为OR 条件。
  • 解决了这个问题!:) 非常感谢拉胡尔
猜你喜欢
  • 2016-09-30
  • 2018-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-21
  • 2015-06-06
  • 1970-01-01
相关资源
最近更新 更多