'Get last ID number from table
I tried this:
<?php
$query = "SELECT MAX(ID) FROM Table";
$result=sqlsrv_query($conn, $query);
$values = sqlsrv_fetch_array($result);
var_dump($values);
echo $values;
?>
But I got this on my webpage:
C:\wamp64\www\site\site.php:18:
array (size=2)
0 => int 1
'' => int 1
Am I missing something?
Solution 1:[1]
$values is an array, so you need to access the direct value if you want to echo it. The max ID is 1, as shown by your var_dump() - but you get two results from the array $values, one associative and one numeric indexed. If you alias your data from the query, you can then fetch the associative value by name of that alias.
<?php
$query = "SELECT MAX(ID) as maxID FROM Table";
$result=sqlsrv_query($conn, $query);
$values = sqlsrv_fetch_array($result);
echo $values['maxID'];
Or if you want to access it numerically,
<?php
$query = "SELECT MAX(ID) FROM Table";
$result=sqlsrv_query($conn, $query);
$values = sqlsrv_fetch_array($result);
echo $values[0];
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Qirel |
