Lorenz Meyer
Lorenz Meyer

Reputation: 19915

Incorrect data in MyISAM database due to concurrency

Problem

I have a webpage that does the following (the code is much simplified to show only relevant code.

mysql_query("insert into table1 (field1) values ('value')");
$last_id = mysql_insert_id();
$result = mysql_query("select * from table1 t inner join ... where id = $last_id");
write_a_file_using_result($result);

It happened, that the file was created using a different data set than what I found in the table row.

The only explanation I have is:

Result: The file is generated with data set 1 while the table row contains data row 2.

Config

mysql 5.0.51b

The table:

CREATE TABLE `table1` (
  `id` int(11) NOT NULL auto_increment,
  (...)

Question

I know that MyISAM does not support transactions. But I really expect that it is impossible to insert two rows and get twice the same id inserted, so that the row can be overwritten.

Is MyISAM unsafe to this point or is there another explanation that I overlook ?

Note

I know the mysql extension for php is outdated, but I did not yet rewrite the application.

Upvotes: 0

Views: 120

Answers (2)

Greenisha
Greenisha

Reputation: 1437

Haven't heard about id issues in MyISAM. You can try to set link identifier when calling last_insert_id, for example

$link = mysql_connect(...);    
mysql_query("insert into table1 (field1) values ('value')",$link);
$last_id = mysql_insert_id($link);
$result = mysql_query("select * from table1 t inner join ... where id = $last_id",$link);
write_a_file_using_result($result);

Upvotes: 0

Your Common Sense
Your Common Sense

Reputation: 157872

Is MyISAM unsafe to this point

No. mysql_insert_id guaranteed to return the right value only.

or is there another explanation that I overlook ?

Most likely. Check your code.

Upvotes: 1

Related Questions