简体   繁体   English

PDO UNION用? 不工作

[英]PDO UNION with ? not working

I am new to SQL and PDO and PHP so I know I am asking a lot of myself. 我是SQL和PDO以及PHP的新手,所以我知道我自己问了很多。 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. 仍然没有冒险...我想结合两个查询的结果,并使用别名使UNION查询的列名相同。 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. 我有以下代码,不能想为什么它不工作。

Both queries work on their own but I get nothing when I combine them with a UNION. 这两个查询都是自己工作的,但是当我将它们与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();
}

Many thanks in anticipation and thank you for your kind attention. 非常感谢您的期待并感谢您的关注。

Bri BRI

由于您有两个占位符 - 您应该将值绑定两次

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

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

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

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

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