MKJ
MKJ

Reputation: 133

Display Last userID in php from MySQL

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;

Upvotes: 0

Views: 80

Answers (2)

スージン
スージン

Reputation: 153

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;

Upvotes: 0

Orange Orange
Orange Orange

Reputation: 1981

/* 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"];

Upvotes: 1

Related Questions