How to return the AUTO_INCREMENT ID generated from the previous INSERT operation in PHP

1 Answer

0 votes
function connect()
{
    $con = mysql_connect("localhost", "user", "password");
    if (!$con)
        return NULL;
    if ( ! mysql_select_db("databse_name", $con))
        return NULL;
    
    return $con;
}
$con = connect();
if (!$con) die("Line: " . __LINE__  . " Error: ". mysql_error());

$sql = "INSERT INTO questions (q_question) VALUES ('$question')";
if ( ! mysql_query($sql, $con))
    die("Line: " . __LINE__  . " Error: ". mysql_error());

$q_id = mysql_insert_id(); // return the AUTO_INCREMENT ID generated from the previous INSERT


answered Jul 26, 2014 by avibootz
edited Jul 26, 2014 by avibootz
...