为什么这个PHP / PDO sqlsrv查询没有返回结果?

时间:2015-04-25 22:20:52

标签: php sql-server pdo

我试图在PHP中使用PDO从mssql数据库表中获取结果,但它没有返回任何结果。但是,我可以计算表格行 - 任何建议吗?

  //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);

表信息:

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
)

2 个答案:

答案 0 :(得分:2)

尝试将子查询移动到主查询,因为在这种情况下执行子查询不会为您做任何事情:

$dbtype = "mysql";
$dbhost = "localhost";
$dbname = "database_name";
$dbuser = "username";
$dbpass = "password";

// Create PDO connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);

// Perform INSERT statement
$sql = "INSERT INTO table VALUES (:value1, :value2)";
$q = $conn->prepare($sql);

// This values are escaped by PDO
$q->execute([':value1' => $data['some_data_1'], ':value2' => $data['some_data_2']));

答案 1 :(得分:2)

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

您正在错误的列上进行过滤。 Id将不会为您做任何事情(否则为什么要打扰ROW_NUMBER()?)。过滤器应位于RowId