Home » Php » Display Last userID in php from MySQL

Display Last userID in php from MySQL

Posted by: admin October 26, 2017 Leave a comment

Questions:

I am trying to echo last userID stored in MySQL. But it is not shown. No error is displayed.

I tried:

$stmti = $reg_user->runQuery("SELECT * FROM tbl_users");
    $rowi = $stmti->fetch(PDO::FETCH_ASSOC);
    $tf = $rowi['userID'];
echo $tf;
Answers:

try this :

$sth = $reg_user->prepare("SELECT MAX(userID) as userID FROM tbl_users");
$sth->execute();
$result = $sth->fetch(PDO::FETCH_ASSOC);
$tf= ($result['userID']);
echo $tf;

Questions:
Answers:

/* NOTE: This works only with mysqli
change the parameters “host”,”username”,”password”,”db_name” with your
own ones
*/

$link = mysqli_connect("host","username","password","db_name");
$query = "  SELECT *
            FROM tbl_users";

$rows = array();
$result = mysqli_query($link, $query);
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) 
    $rows[] = $row;
$lastID = 0;
for ($i = 0 ; $i < count($rows) ; $i++)
     $lastID = $i;
echo $rows[$lastID]["userID"];