开发者

Strange MySQL/PHP issue: mysql query does not execute and there is no error

I ran into a str开发者_C百科ange bug in my PHP web app that I'm unable to sort out. The php script fetches a row from the database, it processes it and serializes a new query and writes back the row to the database.

I copied the code that does this:

foreach($a as $key => $value)
{
    $a[$key]="'".mysql_real_escape_string($value)."'";
}
$a['lastUpdate']="$when";
//assembling request
$assignments=array();
foreach($a as $key => $value)
{
    $assignments[]="$key=$value";
}

$q="UPDATE wtfb2_villages SET ".implode(',',$assignments)." WHERE (id=${a['id']})";
logText($q);
logText(mysql_error());
$r=mysql_query($q) or die(__FILE__.':'.__LINE__.':'.mysql_error().':'.$q);
logText(mysql_affected_rows());
logText('Mysql error in query is: '.mysql_error()."\n");

That UPDATE seemingly executes. mysql_error() gives an empty string. mysql_affected_rows says 1 just like when everything is done. But if I look into the table using phpmyadmin I saw that nothing is changed.

I logged the query itself its:

UPDATE wtfb2_villages SET id='22',ownerId='56',villageName='Új falu',x='0',y='1',buildPoints='7.2226273148149',barracksLevel='0',archeryRangeLevel='0',stablesLevel='0',workshopLevel='0',townHallLevel='0',blacksmithLevel='0',goldmineLevel='1',wallLevel='0',spearmen='0',archers='0',knights='0',catapults='0',diplomats='0',spearmanLevel='0',archerLevel='0',knightLevel='0',catapultLevel='0',spearmenTraining='0',archersTraining='0',knightsTraining='0',catapultsTraining='0',diplomatsTraining='0',lastUpdate='2011-01-18 21:56:10' WHERE (id='22')

If I just copy-paste this query directly into the phpmyadmin it executes it and I see the result in the table.

The table is using myIsam engine. So no transactions or anythin such.

I got stuck at this problem more than 2 hours and I'm unable to find out what's going on.

This is the only one query that does this. queries before and after this one executing properly.

Any ideas?


I found it out... The bug was elsewhere...

When a village is attacked by an army the event processor script first checks whether the village exists. If exists it fetches the row too. Reads the owner's id from the row, and updates the owner's all villages to determine how much loot can be got from that village. After that the event procesor simulates the battle, calculates the casualties and... ...writes back the row with the old update date... pfff That was it...

My advice: if you got stuck with a completely absurd bug... Have a shower, eat your dinner, feed the dog or run a round around the block etc... Just stop thinking... After you sit back in front of the computer you can almost find any kind of bug immediately...

ps: And only when everything fails start a SO post...


Looking at your query log, is ID a primary key? I'm not sure you can include it in your update query if so. It's been a while since I've done DB work though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜