|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-10-25 18:50 UTC] genix at arctoz dot de
Description:
------------
When calling mysql_insert_id() after insert, php returns 0, but if queried "SELECT LAST_INSERT_ID()" it returns the right value.
Test script:
---------------
<?php
mysql_connect("localhost","root","");
mysql_select_db("test");
mysql_query("INSERT INTO table VALUES('', 'TEST')");
echo "LAST-ID (Function): " . mysql_insert_id() . "\n";
$res = mysql_query("SELECT LAST_INSERT_ID() as LID");
$r = mysql_fetch_assoc($res);
echo "LAST-ID (Query): " . $r['LID'];
?>
Expected result:
----------------
LAST-ID (Function): 38
LAST-ID (Query): 38
Actual result:
--------------
LAST-ID (Function): 0
LAST-ID (Query): 38
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 05:00:02 2025 UTC |
The INSERT query is invalid - "table" is a reserved word. Using > CREATE TABLE `table` (id INT AUTO_INCREMENT PRIMARY KEY, foo TEXT) > mysql_query("INSERT INTO `table` VALUES('', 'TEST')"); it works for me. Which isn't surprising considering a bug this obvious would have been noticed a long time ago. Check your MySQL client and server versions and/or give a real, self-contained test script.