pdo prepare语句不适用于限制

时间:2016-04-19 12:43:59

标签: php pdo

我一直在尝试一些事情,但那不起作用。 这是我的请求:它使用数字而不是:limit但是当我尝试使用变量时它没有。 我非常感谢你的帮助或一些线索到哪里看。 干杯, ķ。

导致问题的行:

$req = $bdd->prepare('SELECT * FROM Twit ORDER BY id DESC LIMIT 0, :limit');
$req->bindParam(':limit',$page,PDO::PARAM_INT);

整个代码以防万一:

    <?php
//echo $_COOKIE['pseudo'] ."<br>";
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>

<form action="minichat_post.php" method="POST">
    <label for="pseudo">Pseudo</label>
    <input type="text" name="pseudo" value="<?php
    if (isset($_COOKIE['pseudo'])) {
        echo $_COOKIE['pseudo'];
        }else{echo "";};?>">
    <br /><br />
    <label for="msg">message</label>
    <input type="text" name="msg"><br /><br />
    <input type="submit" value="Envoyer">
</form>

<div class="container">

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

//requête 
$req = $bdd->query('SELECT * FROM Twit ORDER BY id DESC LIMIT 0,10');
//récupération
while ($donnees = $req->fetch()){
    echo '<p><strong>' . htmlspecialchars($donnees['pseudo']) . '</strong> : ' . htmlspecialchars($donnees['msg']) . '</p>';
}
$req->closeCursor();
?>
<form action="" method="GET">
<label for="page">Combien de msg voulez vous voir</label>
<input type="text" name="page">
<input type="submit" value="voir"></input>
</form>

<?php
$page=htmlspecialchars($_GET['page']);

//connection
if (isset($page) && !empty($page)) {
    try
{
    $bdd = new PDO('mysql:host=localhost;dbname=minichat;charset=utf8', 'root', 'root');
}
catch (Exception $e)
{
        die('Erreur : ' . $e->getMessage());
}


//request WHERE MY PROBLEM IS
/*I've tried that - din't work neither
$req = $bdd->prepare('SELECT * FROM Twit ORDER BY id DESC LIMIT 0, ?');
$req->execute(array(htmlspecialchars($_GET['page'])));*/

$req = $bdd->prepare('SELECT * FROM Twit ORDER BY id DESC LIMIT 0, :limit');
$req->bindParam(':limit',$page,PDO::PARAM_INT);


print_r($req);

//fetchingdata

    while ($donnees = $req->fetch()){
        echo '<p><strong>' . htmlspecialchars($donnees['pseudo']) . '</strong> : ' . htmlspecialchars($donnees['msg']) . '</p>';
    }
}

$req->closeCursor();
?>
</div>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

尝试将$ page变量转换为(int)$ page