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:
- Your table doesn't have an auto_increment field
- 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:
- Make sure it has an auto_increment field
- 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
PHP Include Causes White Space at the Top of the Page
PHP - Returning the Last Line in a File
Laravel 5.5 Error Base Table or View Already Exists: 1050 Table 'Users' Already Exists
PHP Try-Catch Blocks: Are They Able to Catch Invalid Arg Types
Datetime Class VS. Native PHP Date-Functions
Get All Orders Ids from a Product Id in Woocommerce
Show Hide Payment Methods Based on Selected Shipping Method in Woocommerce
Php's Array_Map Including Keys
Convert Lat/Longs to X/Y Co-Ordinates
Laravel 5.1: Keep Uploaded File as Old Input
How to Display Special Characters in PHP
Why Is Calling a Function (Such as Strlen, Count etc) on a Referenced Value So Slow
Foreach Value from Post from Form
Multiple Replace (Probably Preg_Replace) of Same String with Array
Composer Install Error - Requires Ext_Curl When It's Actually Enabled