I have this sql query
"DECLARE @two INT SET @two=2 SELECT * FROM test1 WHERE id = @two;"
When processing a request through PDO, everything goes well, as long as I don't use
$this->result->fetch(\PDO::FETCH_ASSOC, \PDO::FETCH_ORI_FIRST);
due to the use of \PDO::FETCH_ORI_FIRST, it gives me an error:
SQLSTATE[HY106]: [Microsoft][ODBC Driver 17 for SQL Server]Fetch type out of range
I definitely need to save FETCH_ORI_FIRST and in order for the query to work correctly, how can I do this?
My Answer:
SELECT * FROM users ORDER BY id DESC
In PHP using PDO, you can fetch the first row of the result set using the FETCH_ORI_FIRST fetch style. Here's an example code snippet:
php
// Assuming $pdo is your PDO object
$stmt = $pdo->query("SELECT * FROM users ORDER BY id DESC");
$row = $stmt->fetch(PDO::FETCH_ORI_FIRST);
print_r($row);
There is no need to use DECLARE in PHP as it is a SQL command used in stored procedures in databases like MySQL. In PHP, you can directly execute SQL queries using PDO without the need for DECLARE.
Rate this post
3 of 5 based on 2787 votesComments