views:

230

answers:

4
$dml = "insert into ... ";
mysql_query($dml,$con);
$Id = isset($row) ? $row['id'] : mysql_insert_id($con);

I saw the record is created,but just can't retrieve the Id.

What's wrong?

EDIT

fixed,it's caused by $row.

A: 

Can you use the internal MySQL SQL function LAST_INSERT_ID() in an SQL query instead?

dig
A: 

From the PHP manual (http://php.net/manual/en/function.mysql-insert-id.php):

Return Values

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

So, while this may seem silly, ensure that the table you are inserting your data into contains a column with the AUTO_INCREMENT property set.

Jordan S. Jones
It's already set.
Shore
A: 

Hi,

Are you sure you have an auto_increment on that "id", in the table you just inserted some data ?

The manual of mysql_insert_id states that :

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

Maybe this explains the problem ?

If not, can you provide us with more informations ? Like the structure of the table, and/or a full insert query ?

Pascal MARTIN
Oh :-( can you edit your question to add some more information, so ? Might help someone have another, better, idea ^^
Pascal MARTIN
The table structure is complex,and the query includes CJK characters.
Shore
+1  A: 

$row must have been be set so its never evaluating the last_insert_id() call

Craig