简体   繁体   中英

Why is this PHP/PDO sqlsrv query not returning results?

I am trying to get results from a mssql database table using PDO in PHP, but it's not returning any results. However, I can count table rows - any advice?

  //this one working fine return 500 recrod in table
    $sql = "SELECT count(*) FROM Content";
    $stmt = $conn->prepare($sql);
    $stmt->execute();
    $num_rows = $stmt->fetchColumn();

     //this one not returning anything 
    $sql = " SELECT c.*  FROM (
            SELECT ROW_NUMBER() OVER(ORDER BY ID) AS RowID,*  FROM Content
            ) AS c 
        WHERE c.ID > :row_start AND c.ID <= :row_end
        ";
       $stmt = $conn->prepare($sql);
       $stmt->bindParam(':row_start', $row_start);
       $stmt->bindParam(':row_end', $row_end);
       $stmt->execute();

    $allsuck = $stmt->fetchAll(PDO::FETCH_COLUMN);
     print_r($allsuck);

table info :

Array
(
    [0] => ID
    [1] => Title
    [2] => Fulldata
    [3] => description
    [4] => Catid
    [5] => language
    [6] => Created
    [7] => Userid
    [8] => MetaKey
    [9] => Thumbnail
    [10] => Thumbnail_desc
    [11] => Hits
    [12] => Active
    [13] => ModifiedDate
    [14] => ModifiedBy
    [15] => Fb_image
    [16] => important
    [17] => hashTags
)

Try moving the subquery to the main query because doing a subquery in this case doesn't do anything for you:

SELECT ROW_NUMBER() OVER(ORDER BY ID) AS RowID,* 
FROM Content 
WHERE Content.ID > :row_start 
AND Content.ID <= :row_end
SELECT * FROM (
  SELECT ROW_NUMBER() OVER(ORDER BY ID) AS RowID,* 
  FROM Content 
) data
WHERE data.RowID > :row_start 
  AND data.RowID <= :row_end

You're filtering on the wrong column. Id will do nothing for you (otherwise why bother with the ROW_NUMBER() ?). The filter should be on RowId

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM