1.我們知道在Php中準(zhǔn)備語句的執(zhí)行有execute(array())的方式,還有bindParam()的方式,但是通過項(xiàng)目發(fā)現(xiàn),當(dāng)準(zhǔn)備語句中含有l(wèi)imit ?,?的時候,就必須用bindParam()這一種方式:
$sql= "select Pic,Description,SaleAccount,SellPrice from Product limit ?,?";
$stmt = $conn->prepare($sql);
$stmt->bindValue(1, $begin-1, PDO::PARAM_INT);
$stmt->bindValue(2, $end-$begin+1, PDO::PARAM_INT);
$stmt->execute();
2.要注意,在limit ?,?語句的前提下,在bindParam()參數(shù)中,當(dāng)是 PDO::PARAM_INT的時候,一定注意要將第二個傳入的參數(shù)保證是整形,通常第二個參數(shù)往往是$_POST["XXX"]得來的一個值,這里要注意要將$_POST[]的值intVal()轉(zhuǎn)換成整形
下面看一種情況:
$sql="select * from test2 limit 0,?";
$stmt=$conn->prepare($sql);
$row=1;
$stmt->bindParam(1,$row,PDO::PARAM_INT);
$stmt->execute();
foreach($stmt as $row){
echo $row["id"];
}
//能得到正確結(jié)果
下面看一種情況:
$sql="select * from test2 limit 0,?";
$stmt=$conn->prepare($sql);
$row='1';
$stmt->bindParam(1,$row,PDO::PARAM_INT);
$stmt->execute();
foreach($stmt as $row){
echo $row["id"];
}
//不能得到正確結(jié)果
3.要注意,在沒有l(wèi)imit ?,?語句的前提下,在bindParam()參數(shù)中,當(dāng)是 PDO::PARAM_INT的時候,第二個傳入的參數(shù)可以是整形也可以是字符串
下面看一種情況:這里bindParam()的第二個參數(shù)是string但是依然能夠成功執(zhí)行的原因是$sql中沒有l(wèi)imit ?,?
$sql="select * from test2 where id=?";
$stmt=$conn->prepare($sql);
$id='4';
$stmt->bindParam(1,$id,PDO::PARAM_INT);
$stmt->execute();
foreach($stmt as $row){
echo $row["id"];
}