简体   繁体   中英

PDO UNION with ? not working

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.

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();
}

Many thanks in anticipation and thank you for your kind attention.

Bri

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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