无法分页:无效的总值(必须是整数> 0)在Php PDO中

时间:2015-07-25 21:20:52

标签: mysql pdo pagination

我正在处理一段时间的查询并坚持执行它。我正在使用位于此处的分页类 Pagination.class

到目前为止,这是我的代码。问题是它一直显示我的错误

if(isset($_REQUEST['maker'])){$makes = $_REQUEST['maker']; }
if(isset($_REQUEST['transmission'])){ $trans = $_REQUEST['transmission']; } 
if(isset($_REQUEST['year_from'])){ $yearf = $_REQUEST['year_from']; } 
if(isset($_REQUEST['year_to'])){ $yeart = $_REQUEST['year_to']; } 
if(isset($_REQUEST['fuel'])){ $fuels = $_REQUEST['fuel']; } 


if($makes == NULL || NULL == $makes)
{
$makes='%';
 }


 if($trans == NULL || NULL == $trans)
{
 $trans='%';
 }


if($fuels == NULL || NULL == $fuels)
{
$fuels='%';
 }




 try{

$conn = new PDO('mysql:host=localhost;dbname=vc', 'root', '');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

 $result = $conn->prepare('SELECT COUNT(*) FROM vehicle WHERE 
(`Make` LIKE :maker) AND(`Transmission` LIKE :transmission)  AND 
(YEAR(STR_TO_DATE(`Year`,"%Y")) BETWEEN (:year_from) AND (:year_to)) AND
 (`Fuel` LIKE :fuel)'); 

$result->bindParam(':maker', $makes, PDO::PARAM_STR);
$result->bindParam(':transmission', $trans, PDO::PARAM_STR);
 $result->bindParam(':year_from', $yearf,PDO::PARAM_STR);
$result->bindParam(':year_to', $yeart, PDO::PARAM_STR);
$result->bindParam(':fuel', $fuels, PDO::PARAM_STR);

$result->execute(); 
  $num_rows=$result->fetchColumn();  

$pages = new Paginator($num_rows,5,array(15,3,6,9,12,25,50,100,250,'All'));
//echo $pages->display_pages();

//echo "<span class=\"\">".$pages->display_jump_menu().$pages->display_items_per_page()."</span>";



  $stmt = $conn->prepare('SELECT * FROM vehicle WHERE 
(`Make` LIKE :maker) AND (`Transmission` LIKE :transmission)  AND  
(YEAR(STR_TO_DATE(`Year`,"%Y")) BETWEEN (:year_from) AND (:year_to)) 
AND (`Fuel` LIKE :fuel) ORDER BY Year DESC LIMIT :start,:end');

$stmt->bindParam(':start', $pages->limit_start, PDO::PARAM_INT);
$stmt->bindParam(':end', $pages->limit_end, PDO::PARAM_INT);
$stmt->bindParam(':maker', $makes, PDO::PARAM_STR);
$stmt->bindParam(':transmission', $trans, PDO::PARAM_STR);
$stmt->bindParam(':year_from', $yearf, PDO::PARAM_STR);
$stmt->bindParam(':year_to', $yeart, PDO::PARAM_STR);
$stmt->bindParam(':fuel', $fuels, PDO::PARAM_STR);



$stmt->execute();
 $rs = $stmt->fetchAll();

无法分页:总值无效(必须是整数&gt; 0)

将Year字段留空时会出现错误。并且当插入year_from和year_to字段时,其工作正常。可以任何人帮助请。被困5天

年度数据库字段日期

问题在于条款

之间

1 个答案:

答案 0 :(得分:0)

如果您将year_fromyear_to参数留空,则$yearf$yeart保持未定义且以下代码

$result->bindParam(':year_from', $yearf,PDO::PARAM_STR);
$result->bindParam(':year_to', $yeart, PDO::PARAM_STR);

会将参数绑定到空字符串,从而产生类似

的查询
BETWEEN ('') AND ('')

结果为空,$num_rows0,分页构造函数中的验证失败。

您应该在php.ini中使用error_reporting=E_ALLdisplay_errors=On开发,这会警告您未定义的变量。