如果数据库中没有值我收到错误

时间:2016-08-05 09:59:49

标签: php mysql pdo

由于某种原因这在wamp上运行得很好但是如果把它放在共享主机上我得到以下错误信息,在某种程度上我猜它是因为db中没有记录所以负值正在传递而这只发生了如果数据库为空。

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1210 Incorrect arguments to mysqld_stmt_execute

我尝试了var_dump,这是输出

var_dump($offset);
var_dump($per_page);
var_dump($uid);

float(-10) 
int(10) 
int(4)

这是我的编码

$query = "SELECT COUNT(*) FROM applied WHERE memberID = :memberID";
        $stmt = $db->prepare($query);
        $stmt->bindParam(':memberID', $uid, PDO::PARAM_INT);
        $stmt->execute();
        $total = $stmt->fetchColumn();    

$pages = ceil($total / $per_page);

$page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
'options' => array(
'default'   => 1,
'min_range' => 1,
),
)));

$offset = ($page - 1)  * $per_page; 

$query = "SELECT * FROM applied WHERE memberID = :memberID ORDER BY id DESC LIMIT :per_page OFFSET :offset";

         $stmt = $db->prepare($query);
         $stmt->bindParam(':per_page', $per_page, PDO::PARAM_INT);
         $stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
         $stmt->bindParam(':memberID', $uid, PDO::PARAM_INT);
         $stmt->execute();

$result = $stmt->fetchAll();

提前感谢,感谢您的时间

1 个答案:

答案 0 :(得分:0)

请用这些

替换php代码
$query = "SELECT COUNT(*) FROM applied WHERE memberID = :memberID";
        $stmt = $db->prepare($query);
        $stmt->bindParam(':memberID', $uid, PDO::PARAM_INT);
        $stmt->execute();
        $total = $stmt->fetchColumn();    

$pages = ceil($total / $per_page);

$page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
'options' => array(
'default'   => 1,
'min_range' => 1,
),
)));

$offset = ($page - 1)  * $per_page; 
if($offset < 0){
$offset = 10;
}
$query = "SELECT * FROM applied WHERE memberID = :memberID ORDER BY id DESC LIMIT :per_page OFFSET :offset";

         $stmt = $db->prepare($query);
         $stmt->bindParam(':per_page', $per_page, PDO::PARAM_INT);
         $stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
         $stmt->bindParam(':memberID', $uid, PDO::PARAM_INT);
         $stmt->execute();

$result = $stmt->fetchAll();