如何使用Joins从数组中删除记录

时间:2014-08-21 20:14:33

标签: php sql arrays join

我有一个包含多个连接的查询。在我获取结果并通过Id-checker运行之后,我希望能够从IDDestination等于$ ID的数组中删除记录。

由于此查询已加入并且我根据其中一个连接表过滤它们,如何根据连接表从阵列中删除这些记录?

我只希望在用户确认之后发生这种情况。

$query = "
select d.IDCourse,
 d.name as course_name,
 d.slug,
 d.short_description,
d.address,
e.city_name,
e.state_code,
d.zip,
e.city_slug,
e.state_slug,
h.IDDestination,
LOWER(e.iso_code) as country_slug, a.*,
b.IDTeetimeType,
c.name as teetime_type,
b.start_time,b.end_time,

(case dayofweek(a.teetime_dt)
        when 1 then `b`.`sun`
        when 2 then `b`.`mon`
        when 3 then `b`.`tue`
        when 4 then `b`.`wed`
        when 5 then `b`.`thu`
        when 6 then `b`.`fri`
        when 7 then `b`.`sat`
    end) AS `price`, g.tax_rate, f.alias
from cart_course_teetimes a
join course_priceplan b
on a.IDCoursePricePlan = b.IDCoursePricePlan
join course_teetime_type c
on b.IDTeetimeType = c.IDTeetimeType
join course d
on b.IDCourse = d.IDCourse
join vw_cities e
on d.IDCity = e.IDCity
join destinations_cities h
on h.IDCity= d.IDCity

LEFT JOIN (SELECT * FROM media_mapping WHERE is_main_item=1 AND IDGalleryType=3) f
    ON d.IDGallery = f.IDGallery
left join course_tax
g on a.IDCourseTax = g.IDCourseTax


where a.IDCart = :cart_id
order by d.name, a.teetime_dt, b.start_time;";
    $prepared = array(
        "cart_id"   => $idCart,
    );
    $conn     = new DBConnection();
    $results  = $conn->fetch($query, $prepared);
    $conn     = null;

    $results = !empty($results) ? $results : array();


    $id = null;

    foreach($results as $row) {
        // Set ID for the first record.
        if($id === null)
            $id = $row['IDDestination'];



        // will stay true, otherwise it's false and we should kill the loop.
        if($id != $row['IDDestination']) {
            $newid=$row['IDDestination'];

           echo "<script type='text/javascript'> emptycart();</script>";
        $query = "DELETE FROM cart_course_teetimes a WHERE  h.IDDestination='.$id.'";
            $res =mysql_query($query) or die (mysql_error());

            break;
        }
    }

1 个答案:

答案 0 :(得分:0)

这是错误的PHP:

   $query = "DELETE FROM cart_course_teetimes a WHERE  h.IDDestination='.$id.'"

您已经在"引用的字符串中,因此. PHP连接运算符不是运算符,它们只是一个句点。

你想要其中任何一个:

   $query = "DELETE FROM cart_course_teetimes a WHERE  h.IDDestination='" . $id . "'";
   $query = "DELETE FROM cart_course_teetimes a WHERE  h.IDDestination='$id'"

现在你正在制作

 ... WHERE h.IDDestination = .42.

这是无效的SQL。

另外,您似乎正在混合数据库库。你有$conn->fetch()这意味着你正在使用其中一个OO db库(mysqli?pdo?custom wrapper?)。但是你然后打电话给mysql_query()。除非您明确地调用mysql_connect(),否则该删除查询将永远不会执行。在其中一个库中建立的数据库连接在任何其他库中都是完全无用的。