php - How To Get The Last Entry From MYSQL Database? -
i trying last id entered database. here code use:
$test_query = "select * table order id desc limit 1"; if ( mysql_query($test_query) ) { echo 'ok!'; $results = mysql_fetch_array($test_query); echo $results['id']; print_r ($results); }
the output have 'ok!'.
what do wrong?
you need use output of mysql_query
in mysql_fetch_array
.
$res = mysql_query($test_query); if ($res === false) { throw new exception("query failed"); } $row = mysql_fetch_array($res); echo $row["id"];
keep in mind reads one row. if want more use while
loop construction can find here: http://php.net/mysql_fetch_array
if did insert
query use mysql_insert_id()
fetch id. feature of mysql. works in conjunction auto_increment
option.
also, if new site you're building use mysqli_*
functions instead of mysql_*
. latter deprecated.
Comments
Post a Comment