duanba7653 2014-01-21 00:52
浏览 52
已采纳

PDO - SQLSTATE [HY093]:UNION查询的参数号无效

I spent my whole evening researching and trying to figure out what's wrong with my search query. I do some wildcard search using union queries and pagination.

$current_page = 0;

$search1 = $search;
$search2 = $search."%";
$search3 = "%".$search."%";

$pdo = DB::connection()->getPdo();

$stmt = $pdo->prepare('
    SELECT id, desc FROM table WHERE desc LIKE :search1 LIMIT :skip, 15
    UNION
    SELECT id, desc FROM table WHERE desc LIKE :search2 LIMIT :skip, 15
    UNION
    SELECT id, desc FROM table WHERE desc LIKE :search3 LIMIT :skip, 15
');

$stmt->bindParam(':search1', $search1);
$stmt->bindParam(':search2', $search2);
$stmt->bindParam(':search3', $search3);
$stmt->bindParam(':skip', $current_page, PDO::PARAM_INT);

$stmt->execute();

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

The first query (without the unions) works fine, or if I remove the :skip parameter, it works fine as well.

Any ideas what's wrong?

  • 写回答

3条回答 默认 最新

  • dongnao9525 2014-01-21 01:16
    关注

    Using named parameters in PDO, you can't use the same name more than once, unless you do:

    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
    

    But if you do that, the values for :skip will be interpolated into the query as quoted strings, which are not valid in the context of LIMIT. In other words the following:

    LIMIT '0', 15
    

    results in syntax error, because LIMIT wants only true integers as its arguments.

    For more examples and explanation, see my answer to Parametrized PDO query and LIMIT clause - not working

    So your choices are to add a separate parameter for each occurrence of :skip:

    $stmt = $pdo->prepare('
        SELECT id, desc FROM table WHERE desc LIKE :search1 LIMIT :skip1, 15
        UNION
        SELECT id, desc FROM table WHERE desc LIKE :search2 LIMIT :skip2, 15
        UNION
        SELECT id, desc FROM table WHERE desc LIKE :search3 LIMIT :skip3, 15
    ');
    
    $stmt->bindParam(':search1', $search1);
    $stmt->bindParam(':search2', $search2);
    $stmt->bindParam(':search3', $search3);
    $stmt->bindParam(':skip1', $current_page, PDO::PARAM_INT);
    $stmt->bindParam(':skip2', $current_page, PDO::PARAM_INT);
    $stmt->bindParam(':skip3', $current_page, PDO::PARAM_INT);
    

    Or else interpolate the value into the query yourself, without quoting the integer.

    But I agree with the comment from @Class, you don't have to do UNION at all. If nothing else, '%search%' works for both of the other two patterns: 'search%' and '%search'. You don't have to search all three.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)