简体   繁体   English

联盟 Select 在 PDO 不工作

[英]Union Select in PDO not working

I'm new to using PDO to make db requests and need a little help.我刚开始使用 PDO 发出数据库请求,需要一些帮助。 I have the following db call:我有以下数据库调用:

$stmt1 = $pdo->prepare('
    SELECT * FROM news WHERE pub_date >= ? AND pub_date < ? AND display = 1 ORDER BY pub_date DESC
    UNION
    SELECT * FROM vs_news WHERE pub_date >= ? AND pub_date < ? AND display = 1 ORDER BY pub_date DESC
');
$stmt1->bindParam(1, $col_start);
$stmt1->bindParam(2, $col_end);
$stmt1->execute();

I have read enough to think the UNION is compatable with PDO, but I can't seem to get the code right and can't find an example in complete code format.我已经阅读了足够多的内容,认为 UNION 与 PDO 兼容,但我似乎无法正确获取代码,也找不到完整代码格式的示例。

The fields in both tables are the same and the db call works with just one or the other table, but not with the UNIION that I have shown.两个表中的字段相同,并且 db 调用仅适用于一个或另一个表,但不适用于我展示的 UNION。

Could someone please point where my problem is?有人可以指出我的问题在哪里吗?

Thanks谢谢

Using the ? 使用 ? means you need to match a param for each ? 意味着您需要为每个参数匹配一个参数?

Use this sort of approach: 使用这种方法:

$stmt1 = $pdo->prepare('
    SELECT * FROM news WHERE pub_date >= :date1 AND pub_date < :date2 AND display = 1 ORDER BY pub_date DESC
    UNION
    SELECT * FROM vs_news WHERE pub_date >= :date1 AND pub_date < :date2 AND display = 1 ORDER BY pub_date DESC
');
$stmt1->bindParam(':date1', $col_start);
$stmt1->bindParam(':date2', $col_end);
$stmt1->execute();

Also; 也; With union, make sure that you use the SAME number of columns in both queries. 使用联合,请确保在两个查询中使用相同的列数。

In order to use ORDER BY inside UNION , you should enclose the components in () . 为了在UNION使用ORDER BY ,您应该将组件包含在() Do this in addition to binding all four parameters as suggested in the comments, or using named place holders as suggested elsewhere: 除了按照注释中的建议绑定所有四个参数之外,或者按照其他地方的建议使用命名的占位符,还可以这样做:

$stmt1 = $pdo->prepare('
    (SELECT * FROM news WHERE pub_date >= ? AND pub_date < ? AND display = 1 ORDER BY pub_date DESC)
    UNION
    (SELECT * FROM vs_news WHERE pub_date >= ? AND pub_date < ? AND display = 1 ORDER BY pub_date DESC)
');

This is described in the MySQL UNION syntax reference. MySQL UNION语法参考中对此进行了描述

I like Menztrual's answer.我喜欢 Menztrual 的回答。 It's the one I like the most because it gives me more control over the visual distribution of my elements in a query, however, the fun part comes after in the binding of the data, I always find to complicated having to write a bindParam for every placeholder这是我最喜欢的一个,因为它让我可以更好地控制查询中元素的视觉分布,但是,有趣的部分出现在数据绑定之后,我总是发现必须为每个元素编写一个 bindParam 很复杂占位符

$stmt1->bindParam(':date1', $col_start);
$stmt1->bindParam(':date2', $col_end);
$stmt1->execute();

So, I use a much simpler and elegant way of doing this.所以,我使用了一种更简单、更优雅的方法来做到这一点。 I'll leave here an example of a simpler binding array way of execution for anyone that doesn't already know about it.我将在这里留下一个更简单的绑定数组执行方式的示例,供任何尚不了解它的人使用。

$stmt1->execute([':date1'=>$col_start,':date2'=>$col_end]);

For cooler facts on PDO I recommend this site: (The only proper) PDO Tutorial lots of useful content there.对于 PDO 上更酷的事实,我推荐这个网站:(唯一正确的)PDO 那里有很多有用的教程教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM