2013-10-08 48 views
1

我遇到了分页问题。我已经尽可能地进行了调试,看起来SELECT查询和执行有问题。如果我取出分页部分,查询将执行并显示长表中的所有条目。我尝试执行一个数组,bindValue和bindParam,但没有任何工作,任何人都可以看到我失踪?PHP OOP分页

function showEmployees() { 
    $count = $this->dbh->query("SELECT * FROM employee_info"); 
    $count->rowCount(); 
    $count = $count->rowCount(); 

    // if ($count > 0) { 
    // echo "The total amount of employees is " . $count; 
    // } else { 
    // echo "There are no records in this table."; 
    // } 

    $page_rows = 10; 
    $last_page = ceil($count/$page_rows); 

    echo $last_page; 

    if ($last_page < 1) { 
    $last_page = 1; 
    } 

    $page_num = 10; 

    if (isset($_GET['pn'])) { 
    $page_num = preg_replace('#[^0-9]#', '', $_GET['pn']); 
    } 

    if ($page_num < 1) { 
    $page_num = 1; 
    } elseif ($page_num > $last_page) { 
    $page_num = $last_page; 
    } 

    $limit = 'LIMIT ' .($page_num -1) * $page_rows .', '. $page_rows;  

    $query = $this->dbh->prepare("SELECT * FROM employee_info ORDER BY employee_id DESC :page_limit"); 
    $query->bindParam(':page_limit', $limit); 
    $query->execute(); 


    $t = "<table name='displayEmployees' border='1' >"; 
    $t .="<tr>"; 
    $t .= "<th>Employee ID</th>"; 
    $t .= "<th>First Name</th>"; 
    $t .= "<th>Last Name</th>";    
    $t .= "</tr>"; 

    while($u = $query->fetch(PDO::FETCH_ASSOC)) { 
    $t .="<tr>"; 
    $t .= "<td>{$u['employee_id']}</td>"; 
    $t .= "<td>{$u['first_name']}</td>"; 
    $t .= "<td>{$u['last_name']}</td>"; 
    $t .="</tr>"; 
    } 

    $t .="</table>"; 

    return $t; 
}  

回答

0

您不能将关键字作为您的预准备语句的参数。你需要做到这一点:

$limit = 'LIMIT ' .($page_num -1) * $page_rows .', '. $page_rows;  
$query = "SELECT * FROM employee_info ORDER BY employee_id DESC ".$limit; 
$query->query($query); 

注:因为这些参数已通过你的脚本消毒你可以信任他们,所以准备好的声明的利益在这种情况下,主要是丢失。

+0

感谢迈克,我明白你在说什么,并欣赏它,由于注射的偏执我有这么多为我准备做不假思索所有发言的习惯,但你是正确的,在这种情况下,这是多余的,抱歉,我还没有被允许投票。 – Jake

1

它认为这是你处理限制的方式,尽管这应该会触发一个错误。

尝试:

$beginLimit = ($page_num-1)*$page_rows; 
$endLimit = $page_rows; 
$query = $this->dbh->prepare("SELECT * FROM employee_info ORDER BY employee_id DESC LIMIT :begin,:end"); 
$query->bindValue(':begin',(int)$beginLimit,PDO::PARAM_INT); 
$query->bindValue(':end',(int)$endLimit,PDO::PARAM_INT); 
$query->execute(); 
+0

非常感谢凯西,不幸的是我没有被允许投票,但这是非常感谢。 – Jake