Mysql_Insert_Id() Returns 0

mysql_insert_id() returns 0

According to the manual mysql_insert_id returns:

The ID generated for an AUTO_INCREMENT column by the previous query on
success, 0 if the previous query does not generate an AUTO_INCREMENT
value
, or FALSE if no MySQL connection was established.

Since it does not give you false and not the correct number it indicates that the queried table didn't generate an auto-increment value.

There are two possibilities I can think of:

  1. Your table doesn't have an auto_increment field
  2. Since you doesn't provide the link to the mysql_insert_id() but using a link with mysql_query() it might not be the correct table that's queried when retrieving the last inserted id.

Solution:

  1. Make sure it has an auto_increment field
  2. Provide the link aswell: $waarde = mysql_insert_id($this->db);

Why does mysql_insert_id() return 0?

you need to actually run the query first:

$sql= "INSERT INTO `database`.`table`(Columns) VALUES (Values)...";
$result = mysql_query($sql);

and then after that:

$id = mysql_insert_id();
echo $id

Let me know if you have still problems.

mysql_insert_id() always returns 0

Do mysql_insert_id after mysql_query.

Look the documentation here, and try to not use the mysql* functions anymore, as it is explained in the red box.
There are securities issues with these function.

mysql_insert_id : Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).

In your code, when you do mysql_insert_id, no insert was made.

mysql_insert_id() is returning 0

You're mixing up mysql and mysqli

$message = mysql_insert_id();

should be

$message = mysqli_insert_id($dbc);

mysql_insert_id() always returning 0

You forgot to run your query and you've mixed mysql_ and mysqli_ APIs

$sql=" INSERT INTO Documents (doc_name, doc_type, doc_manager, doc_pointer) VALUES ('$upload_name','Privacy','Agente di riferimento','$pic')";
mysqli_query($con, $sql);
$last_id = mysqli_insert_id($con);

FYI, this example has no error handling. You should add it.

mysql_insert_id returning only 0

You've switched database libraries. Use mysqli_ throughout, it isn't compatible with the deprecated mysql_ library.

  • http://php.net/manual/en/mysqli.insert-id.php

mysql_insert_id() echos 0

OK I finally made it!
In my Class I added the function insert_id() and then added the insert_id variable locally.

public function insert_id()
{
$result = $this->MySQLiObj->insert_id;
return $result;
}


Related Topics



Leave a reply



Submit