2015-01-20 2 views
0

이 페이지 매김 테이블 설정을 얻으려고하고 있습니다. 그러나이 오류가 발생하는 이유는 무엇입니까? 지금은 MySQL에서 작동하지만 MSSQL으로 전환 할 때입니다. MySQL의 속성 이름을 인용하는 데 사용하는 ` 문자는mssql을 사용하여 페이지 매김을 가져 오는 문제

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near '`'.' in C:\inetpub\wwwroot\pagination\index.php:40 Stack trace:#0 C:\inetpub\wwwroot\pagination\index.php(40): PDOStatement->execute()#1 {main}thrown in C:\inetpub\wwwroot\pagination\index.php on line 40 Line 40 is near the Stmt2 execute.

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8"> 
    <title>Receiving</title> 
     <!-- CSS File --> 
    <link href="css/pagination.css" rel="stylesheet"> 
    </head> 
<body> 
<?php 
require_once ("db_connect.php"); 

// No. of adjacent pages shown on each side 
$adjacents = 2; 
// We will assign variable here for entry By. you can use your variables here. 
$EntryBy = "completed"; 
// We Will prepare SQL Query 
$STM = $dbh->prepare("SELECT DateRequested FROM receivingrequests WHERE Status = :Status"); 
// bind paramenters, Named paramenters alaways start with colon(:) 
$STM->bindParam(':Status', $EntryBy); 
// For Executing prepared statement we will use below function 
$STM->execute(); 
// Count no. of records 
$Records = $STM->rowCount(); 
// Your File Name will be the same like your php page name which is index.php 
$targetpage = "index.php"; 
// Below is setting for no. of records per page. 
$limit = 10; 
$page = $_GET['page']; 
if ($page) 
// First Item to dipaly on this page 
$start = ($page - 1) * $limit; 
    else 
// if no page variable is given, set start to 0 
$start = 0; 
// Get data using PDO prepare Query. 
$STM2 = $dbh->prepare("SELECT `RequestNumber`,`DATEREQUESTED`, `EmpName` ,`Department` ,`VasLblDate` ,`Status` FROM receivingrequests WHERE Status = :Status ORDER BY RequestNumber LIMIT $start, $limit"); 
// bind paramenters, Named paramenters alaways start with colon(:) 
$STM2->bindParam(':Status', $EntryBy); 
// For Executing prepared statement we will use below function 
$STM2->execute(); 
// We will fetch records like this and use foreach loop to show multiple Results later in bottom of the page. 
$STMrecords = $STM2->fetchAll(); 
// Setup page variables for display. If no page variable is given, default to 1. 
if ($page == 0) $page = 1; 
// previous page is page - 1 
$prev = $page - 1; 
// next page is page + 1 
$next = $page + 1; 
// lastpage is = total Records/items per page, rounded up. 
$lastpage = ceil($Records/$limit); 
// last page minus 1 
$lpm1 = $lastpage - 1; 
// Now we apply our rules and draw the pagination object. We're actually saving the code to a variable in case we want to draw it more than once. 
$pagination = ""; 
if ($lastpage > 1) 
    { 
    $pagination.= "<div class='pagination'>"; 
    // previous button 
    if ($page > 1) $pagination.= "<a href='$targetpage?page=$prev'>Previous</a>"; 
     else $pagination.= "<span class='disabled'>Previous</span>"; 
    // pages 
    if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up 
     { 
     for ($counter = 1; $counter <= $lastpage; $counter++) 
      { 
      if ($counter == $page) $pagination.= "<span class='current'>$counter</span>"; 
       else $pagination.= "<a href='$targetpage?page=$counter'>$counter</a>"; 
      } 
     } 
    elseif ($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some 
     { 
     // close to beginning; only hide later pages 
     if ($page < 1 + ($adjacents * 2)) 
      { 
      for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) 
       { 
       if ($counter == $page) $pagination.= "<span class='current'>$counter</span>"; 
        else $pagination.= "<a href='$targetpage?page=$counter'>$counter</a>"; 
       } 
      $pagination.= "..."; 
      $pagination.= "<a href='$targetpage?page=$lpm1'>$lpm1</a>"; 
      $pagination.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>"; 
      } 
     // in middle; hide some front and some back 
     elseif ($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) 
      { 
      $pagination.= "<a href='$targetpage?page=1'>1</a>"; 
      $pagination.= "<a href='$targetpage?page=2'>2</a>"; 
      $pagination.= "..."; 
      for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) 
       { 
       if ($counter == $page) $pagination.= "<span class='current'>$counter</span>"; 
        else $pagination.= "<a href='$targetpage?page=$counter'>$counter</a>"; 
       } 
      $pagination.= "..."; 
      $pagination.= "<a href='$targetpage?page=$lpm1'>$lpm1</a>"; 
      $pagination.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>"; 
      } 
     // close to end; only hide early pages 
      else 
      { 
      $pagination.= "<a href='$targetpage?page=1'>1</a>"; 
      $pagination.= "<a href='$targetpage?page=2'>2</a>"; 
      $pagination.= "..."; 
      for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) 
       { 
       if ($counter == $page) $pagination.= "<span class='current'>$counter</span>"; 
        else $pagination.= "<a href='$targetpage?page=$counter'>$counter</a>"; 
       } 
      } 
     } 
    // next button 
    if ($page < $counter - 1) $pagination.= "<a href='$targetpage?page=$next'>Next</a>"; 
     else $pagination.= "<span class='disabled'>Next</span>"; 
    $pagination.= "</div>\n"; 
    } 
// Below is a start of table in which we will show records using foreach loop. 
// We use foreach loop here to echo records. 
foreach($STMrecords as $r) 
    { 
    echo "<table width='100%' class='mytableP'>"; 
    echo "<tr><th>RequestNumber</th><th>DateRequested</th><th>Employee Name</th><th>Department</th><th>VasLablDate</th><th>Status</th></tr>"; 
    echo "<tr>"; 
    echo "<td>" . $r[0] . "</td>"; 
    echo "<td>" . $r[1] . "</td>"; 
    echo "<td>" . $r[2] . "</td>"; 
    echo "<td>" . $r[3] . "</td>"; 
    echo "<td>" . $r[4] . "</td>"; 
    echo "<td>" . $r[5] . "</td>"; 
    echo "</tr>"; 
    echo "</table>"; 
    echo "<br />"; 
    } 
// For showing pagination below the table we will echo $pagination here after </table>. For showing above the table we will echo $pagination before <table> 
echo $pagination; 
// Closing MySQL database connection 
$dbh = null; 
?> 
    </body> 
+3

'LIMIT'은 SQL Server 구문이 아닌 MySQL 구문입니다. SQL Server 2012 이상에서는 [OFFSET/FETCH] (http://sqlperformance.com/2015/01/t-sql-queries/pagination-with-offset-fetch)를 사용하거나 [더 많은 구식 구 버전의 메소드] (http://www.sqlservercentral.com/articles/paging/69892/)를 참조하십시오. –

답변

1

내가 오류의 원인을 의심 :이 오류가 발생합니다. MSSQL은 []을 사용합니다.

'문자 만 삭제하면 예약어를 표 속성으로 사용하지 않는 한 별다른 문제가되지 않습니다.

편집 : LIMIT 문제는 : 간단한 PDO 랩퍼 인 doctrine DBAL 라이브러리를 사용할 수 있습니다. 핵심 구문은 PDO 구문과 같지만 당신은 니스 querybuilder 같은 기능을 가지고 : 당신이 사용할 수있는 querybuilder와 http://doctrine-dbal.readthedocs.org/en/latest/reference/query-builder.html

->setFirstResult()와 MySQL에서 제한 기능을 에뮬레이션 ->setMaxResults().

그러나 @AaronBertrand가 위에서 언급 한 내용 (OFFSET/FETCH 사용)은 SQL Server 2012 이상을 사용하면 훨씬 쉽습니다.

+0

그 구문을 제외하고, 스크립트는 여전히 'LIMIT'이 SQL Server에서 유효하다고 가정합니다. –

+0

그건 그렇고, 나는 그 자체가 코드를 포맷하는 데 사용되는 것처럼 내 대답에'as 코드를 형식화하기 위해 시행 착오가 필요했다. 이것은 작동했다 : \\'

+0

나는 무엇으로 한계를 대체 할텐데. 나는이 방법을 mssql과 함께 사용하는 방법에 대해서 혼란스럽지 않다. – Veronica

관련 문제