PHP mysql query code not working
this code is not working why :(
$id = $temp['curchar'];
$data=strtotime(date('Y-m-d H:i:s'))+30; //+30 seconds to unix time
mysql_query("UPDATE `chars` SET data='$data' WHERE id='$id'");
Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in C:\Program Files\WebServ\httpd\world_1开发者_运维问答\char_info_slow.php on line 23
too many questions, you probably can start by checking
- how you connect to mysql?
- what is the column type for
data
? - is
$id
match any record in table? - how to verify are the matched records get updated?
- if your account connect to mysql allow to do write?
ps:
$data=strtotime(date('Y-m-d H:i:s'))+30; <-- wordless ...
$data = time()+30;
pps:
at least, you should try
$sql = "UPDATE `chars` SET data='$data' WHERE id='$id'";
mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
There's no reason to generate a date in PHP just to do some date arithmetic in MySQL. You can do this far easier within mysql as is:
UPDATE chars
SET data=DATE_ADD(data, INVERVAL 30 SECOND)
WHERE id=$id
Of course, this assumes you've made data
a datetime type field. If it's just an int, then why bother with all the date math, and just do data=data+30
.
As well, you're generating your time value in a highly inefficient manner. You format the current date as a string, convert that string to a number, and add 30 to it. Why not just do
$data = time() + 30;
instead? time returns the current date/time as a single integer (a unix timestamp), saving you the round trip through String Land.
精彩评论