-4

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?

8
  • You only get this error when you use LIMIT :limit?
    – Barmar
    Commented Nov 13 at 21:08
  • Have you tried setting the PDO::ATTR_EMULATE_PREPARES option explicitly?
    – Barmar
    Commented Nov 13 at 21:09
  • yes, if i remove the LIMIT, i got no error, how to use "PDO::ATTR_EMULATE_PREPARES"?
    – yanuck
    Commented Nov 13 at 21:12
  • $db->setAttr(PDO::ATTR_EMULATE_PREPARES, true);
    – Barmar
    Commented Nov 13 at 21:13
  • 1
    I wonder if this has something to do with FreeTDS. I think most use the ODBC driver for SQL-Server.
    – Barmar
    Commented Nov 13 at 21:24

1 Answer 1

0

try this query using top

$Limit = 5;
$sql = 'SELECT TOP :limit * FROM [dbo].[table_name] ORDER BY [count] DESC';
$stmt = $db->prepare($sql);
$stmt->bindValue(':limit', (int) $Limit, PDO::PARAM_INT);
$results = $stmt->execute();
1
  • still getting same error :/
    – yanuck
    Commented Nov 13 at 21:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.