0

I have a heap based table in MySQL that I am trying to update via PHP, but for some reason, the updates do not seem to be taking place.

Here is my test code:

<?php
$freepoints[] = 1;
$freepoints[] = 2;
$freepoints[] = 3;
foreach ($freepoints as $entrypoint) {
$query = "update gates set lane='{$entrypoint}' where traffic > 50 limit 50";
echo "$query\n";
mysql_query($query);
echo mysql_affected_rows()."\n";
}
?>

This outputs the following:

update gates set lane='1' where traffic > 50 limit 50
50
update gates set lane='2' where traffic > 50 limit 50
50
update gates set lane='3' where traffic > 50 limit 50
50

In the database to start with lanes 1/2/3 had 0 records and lanes 4/5/6 had 100 records. From this I am expecting all 6 lanes to now have 50 records each. However when I look lanes 4/5/6 still have 100 records and 1/2/3 still have 0 records.

When I copy the query "update gates set lane='1' where traffic > 50 limit 50" into phpMyAdmin it works absolutely fine, so any ideas why it isn't working in my PHP script when mysql_affected_rows is saying it has updated 50 records?

1 Answer 1

1
  1. Add ORDER BY clause
  2. you are always rewriting with the same data. you need to specify WHERE with lane to update another rows
Sign up to request clarification or add additional context in comments.

3 Comments

I changed query to the following but still doesn't work: update gates set lane='{$entrypoint}' where traffic > 50 and lane != '{$entrypoint}' order by lane limit 50
Oh now I feel such an idiot when I realise the issue you're saying. I changed query as follows and it works fine now: "update gates set lane='{$entrypoint}' where traffic > 50 and lane > '{$entrypoint}' limit 50". Thank you!
even it's work for you now - i advice to always specify ORDER to get obvious results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.