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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动