1

I just Want to Get Newest Record From a Database Table and i got this error when i used LIMIT how can i solve this?

Connection established.

Array ( [0] => Array ( [0] => 42000 [SQLSTATE] => 42000 [1] => 102 [code] => 102 [2] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near 'LIMIT'. [message] => [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near 'LIMIT'. ) )


$query = 'SELECT EventTime, EventHumidity, EventTemperature FROM TxnFMISHumidityTempEvent ORDER BY EventID DESC LIMIT 1';
$stmt = sqlsrv_query( $conn, $query );
if( $stmt === false) 
{
    die( print_r( sqlsrv_errors(), true) );
}

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) 
{
    $css_class=' class="row"';
    echo "<tr '.$css_class.'>";
    echo "<td>SNSRP001</td>";
    echo "<td>MIS</td>";                
    echo "<td>" .$Date = $row['EventTime']->format('Y-m-d H:i:s'). "</td>";
    echo "<td>" .$row['EventHumidity'].  "</td>";
    echo "<td>" .$row['EventTemperature'].  "</td>";
    echo "</tr>";
}
sqlsrv_free_stmt( $stmt);
TarangP
  • 2,560
  • 5
  • 18
  • 35

2 Answers2

0

LIMIT is not supported in SQL Server, you can use TOP 1 instead of LIMIT with ORDER BY.

Change your @query like following.

$query = 'SELECT TOP 1 EventTime, EventHumidity, EventTemperature FROM TxnFMISHumidityTempEvent ORDER BY EventID DESC';
PSK
  • 15,515
  • 4
  • 25
  • 38
0

try select * to select all data.

SELECT * FROM TxnFMISHumidityTempEvent ORDER BY primaryKeyId DESC LIMIT 1;
PHP Geek
  • 2,822
  • 1
  • 11
  • 25