php - PDO getting only one value -


i querying database latest row entered , id column only, sure 1 value. however, unable print variable unless in foreach loop, not accessing 0th element giving me output.

i use line of code querying latest row's id added: $id = $db->query("select max(id) fm;");

when use foreach loop, following output:

loop:

foreach ($id $i)     print_r($i); 

output:

array ( [max(id)] => 20 [0] => 20 ) 

however, when use print $id[0]; or print $id["max(id)"]; http error 500.

how can single value id , put in variable, id is?

thanks

instead of selecting max(id) have use lastinsertid() method

$id = $db->lastinsertid();  

right after calling insert query


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -