PDO UNION与?不工作 [英] PDO UNION with ? not working

查看:97
本文介绍了PDO UNION与?不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是SQL,PDO和PHP的新手,所以我知道我在问自己很多.仍然没有冒险... 我想结合两个查询的结果,并且正在使用别名使UNION查询的列名称相同. 我尝试了各种减少操作,直到没有减少的余地为止,我的应用程序需要的实际结果还有很多.我有以下代码,无法考虑为什么它不起作用.

I am new to SQL and PDO and PHP so I know I am asking a lot of myself. Still nothing ventured... I want to combine the results of two queries and am using aliases to make the column names the same for the UNION query. I have tried all sorts of reducing till there is nothing left to reduce, there are many more in the actual result I need for my app. I have the following code by can't think why it is not working.

这两个查询都可以独立工作,但是当我将它们与UNION结合使用时我什么也没得到.任何建议都是最有帮助的.

Both queries work on their own but I get nothing when I combine them with a UNION. Any suggestions would be most helpful.

include ("conn/dbCon_1.php");
$sql= "(SELECT  First_Name AS myResult FROM tbl_User WHERE First_Name LIKE ?)";
$sql.=" UNION ALL ";
$sql.= "(SELECT Course_Title AS myResult FROM tbl_Course  WHERE Course_Title LIKE ? )";
$c1 =  "%le%";
try {
    $qResults = $dbConn->prepare($sql); 
    $qResults->execute([$c1]); 
    $qResults->fetch(PDO::FETCH_ASSOC);
    return $qResults;
    //Close connection
    $dbConn = null;
} catch(PDOExcepetion $e) {
    echo $e->getMessage();
}

非常感谢您的光临.

布里

推荐答案

即使两个参数具有相同的值,您也可以使用两个参数来调用该查询(例如,在第一个查询中和在第二个查询中),因此您必须传递两个参数

You invoke the query with two parameters (like in the first Query and like in the second) even if they have the same value .. so you have to pass two parameters

$qResults->execute([$c1, $c1]);

这篇关于PDO UNION与?不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆