php - 准备好的 SQL 查询、UNION 和 LIMIT - 语法

标签 php mysql sql

我在使用限制时遇到 SQL 查询问题。我想要一个可变的限制参数。正如我在网站的其他地方成功完成的那样,我想使用类似 LIMIT number = :number 的东西,然后指定 'number'=>$numberResults。

不知何故,这不起作用..(我已经尝试过,如果我在查询中写出 LIMIT 20 它工作正常,但这不是问题。

这是我的代码:

connexion_bdd.php

<?php
        try
        {
            $bdd = new PDO('mysql:host=localhost;dbname=mise_a_quai_tardive', 'root', '');
        }
        catch (Exception $e)
        {
                die('Erreur : ' . $e->getMessage());

        }
 ?>

mise_a_quai_tardive.php 这给我带来了问题

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="style.css" />
    <title>Module Suivi des Mises à Quai Tardives</title>
</head>

<body>
<?php include("connexion_bdd.php"); ?>
<?php
  $resultatsParPage = 15;
$reponse = $bdd->prepare("SELECT id_dossier, type_sortie, date_incident, no_train,    commentaire FROM sortie_prevue
        UNION 
        SELECT id_dossier, type_sortie, date_incident, no_train, commentaire FROM sortie_non_prevue
        ORDER BY date_incident DESC LIMIT limit=:limit")

$reponse -> execute(array('limit'=> $resultatsParPage));

while ($donnees = $reponse->fetch())
{

/* i display the results here, they display fine with a 'static' SQL query, but nothing shows up with my query at the moment.. */

}

</body>
</html>

非常感谢您的帮助!我相信对此有一个简单的解释! :)

最佳答案

奇怪的是,这似乎有效...我直接执行查询,没有任何准备。

$reponse = $bdd->query("SELECT id_dossier, type_sortie, date_incident, no_train, commentaire FROM sortie_prevue
        UNION 
        SELECT id_dossier, type_sortie, date_incident, no_train, commentaire FROM sortie_non_prevue
        ORDER BY date_incident DESC LIMIT $resultatsParPage");

如果有人有评论来解释这一点,他将非常欢迎!

关于php - 准备好的 SQL 查询、UNION 和 LIMIT - 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18961678/

相关文章:

sql - 笛卡尔连接在现实生活中在哪里使用?

javascript - 拖放(事件)不触发ajax POST

php - 如何在 laravel 中获取 http referer?

MySQL:将 1:m 的所有值放入一个字段中?

java - 如何在 hibernate 中使用一对一映射执行插入

mysql - SQL计算两个不同表的计数之间的比率

javascript - 如何使用 href 调用函数?

php - 通过扩展的 php 生成 4 位 token

PHP:my_thread_global_end() 错误:1 个线程未退出

mysql - sql select查询优化