here my code :
$Limit=5;
$sql = 'SELECT * FROM [dbo].[table_name] ORDER BY [count] DESC LIMIT :limit';
$stmt = $db->prepare($sql);
$stmt->bindValue(':limit', (int) $Limit, PDO::PARAM_INT);
$results = $stmt->execute();
and here the error im getting :
Fatal error: Uncaught PDOException: SQLSTATE[HY090]: Invalid string or buffer length: [FreeTDS][SQL Server]Invalid string or buffer length
Any idea what im doing wrong?
2
Answers
try this query using top
The error you are encountering is likely due to the fact that
LIMIT
is not supported in Microsoft SQL Server, which is indicated by the error message you received.To achieve similar functionality in SQL Server, you can make use of the
OFFSET
andFETCH
clauses. Here is how you can modify your SQL query to achieve pagination in SQL Server:By using
OFFSET
andFETCH NEXT
, you can achieve pagination in SQL Server without usingLIMIT
. Adjust the$Offset
value as needed to fetch different pages of data.Make sure that your PHP PDO connection is set up correctly to work with Microsoft SQL Server, and that your database table names and column names are correct in your query.
Also, please check your SQL Server driver and version to ensure that it supports the
OFFSET
andFETCH NEXT
clauses.