When I execute the following in the MySQL console, it works fine:
INSERT INTO videos (embed_code) VALUES ('test code here');
SELECT LAST_INSERT_ID();
But, when I execute the above queries via PHP, the result is empty.
Under the data abstraction, I am using a database access class named DB
. This is how I have tried the above queries via PHP:
$embedCode = htmlentities($_POST['embed_code']);
//Insert video in database **WORKS**
DB::query("INSERT INTO videos (embed_code) VALUES ('$embedCode');");
//Retrieve id **DOES NOT WORK**
$row = DB::getSingleRow("SELECT LAST_INSERT_ID();");
$videoId = $row[0];
It is not reliable for me to select the new id by the embed_code, as the embed_code could be repeated.
Thanks!
Doesn't your database class contain a function to grab the last insert ID? Try mysql_insert_id() otherwise instead.