Search code examples
phpmysqlmysqlimysql-insert-id

Return ID of column from table with mysql_insert_id


$sql="INSERT INTO survey (user, title, description, opta, optb,optc,optd,time) VALUES ('$user','$title', '$dis', '$a' , '$b', '$c', '$d','timespan')";
if (mysqli_query($con,$sql))
 {
  echo "Success";
 }
 else
  {
  echo "Error: " . mysql_error();
  }
  echo $id = mysql_insert_id();

mysqli_close($con); 

I want to return id of column from table, but it returns 0. why? and also how can i fix this problem? thanks in advance


Solution

  • You're mixing mysql and mysqli functions. Change:

    echo $id = mysql_insert_id();
    

    to

    echo $id = mysqli_insert_id($con);
    

    You also need to change mysql_error() to mysqli_error()

    Not doing so, will not signal any possible errors found.