dj-kenpo Posted July 27, 2007 Share Posted July 27, 2007 can't figure out what I'm doing wrong here and it's really annoying! $month = date("F"); $date = date("d"); $year = date("Y"); $Timestamp_today = strtotime ("$month $date $year"); $query ="UPDATE site_stats_bots SET clicks = clicks+1 WHERE Timestamp='$Timestamp_today' AND User_ID='$User_ID'"; $result = mysql_query($query)or print ("Can't update<br />" . mysql_error()); if ($result == false) { $query = "insert into site_stats_bots (User_ID, clicks, Timestamp) values ('$User_ID', '1', '$Timestamp_today' )"; mysql_query($query); } I want it to update the bots stats for today, and if there is no row for today, create one. the query runs, but there's NO ERROR 'or print ("Can't update<br />" . mysql_error());' yet, no data goes into the databse. no data, no error, it just silently ignors me. Quote Link to comment Share on other sites More sharing options...
envexlabs Posted July 27, 2007 Share Posted July 27, 2007 i think your missing a variable $query = "insert into site_stats_bots (User_ID, clicks, Timestamp) values ('$User_ID', '1', '$Timestamp_today' )"; $result = mysql_query($query); } Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted July 27, 2007 Share Posted July 27, 2007 How is the field Timestamp declared? Quote Link to comment Share on other sites More sharing options...
dj-kenpo Posted July 27, 2007 Author Share Posted July 27, 2007 envexlabs, result isn't needed there as it's the insert, by that point I don't need to know the result. roopurt18: timestamp is int(15). the querys individually work a-ok, but inside the script it ceases to work. it doesn't seem to return false for some reason. even in phpmyadmin, I can call the update command on an empty table and while it doesn't work, it also doesn't complain with any errors... Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 27, 2007 Share Posted July 27, 2007 Did you mean that if you deliberately put an error in the first query, you still dont get any error message telling you that the query failed? If so, i can only assume that this piece of code is not being exectued anyway - perhaps there is some other logical error in your script? Quote Link to comment Share on other sites More sharing options...
per1os Posted July 27, 2007 Share Posted July 27, 2007 OK Let's think here. $month = date("F"); $date = date("d"); $year = date("Y"); $Timestamp_today = strtotime ("$month $date $year"); $query ="UPDATE site_stats_bots SET clicks = clicks+1 WHERE Timestamp='$Timestamp_today' AND User_ID='$User_ID'"; $result = mysql_query($query)or print ("Can't update<br />" . mysql_error()); if (mysql_affected_rows($result) < 1) { $query = "insert into site_stats_bots (User_ID, clicks, Timestamp) values ('$User_ID', '1', '$Timestamp_today' )"; mysql_query($query); } Checking if $result is false does not work. Reason being, an update statement can update 0 rows and it is still a valid SQL query. Use www.php.net/mysql_num_rows to check if the query actually did any work/updating. Sometimes it is always the smallest things. EDIT: Changed to ww.php.net/mysql_affected_rows since it is an update statement. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 27, 2007 Share Posted July 27, 2007 Ah yes, of course. But wont you need mysql_affected_rows rather than mysql_num_rows, since this is an update? Quote Link to comment Share on other sites More sharing options...
per1os Posted July 27, 2007 Share Posted July 27, 2007 Ah yes, of course. But wont you need mysql_affected_rows rather than mysql_num_rows, since this is an update? I honestly do not know. I think either will work. EDIT: Retrieves the number of rows from a result set. This command is only valid for statements like SELECT or SHOW that return an actual result set. To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows(). You learn something new every day =) Quote Link to comment Share on other sites More sharing options...
dj-kenpo Posted July 27, 2007 Author Share Posted July 27, 2007 Checking if $result is false does not work. Reason being, an update statement can update 0 rows and it is still a valid SQL query. AHH! I didn't know but it all makes sense now!! that's why the query worked fine in phpmyadmin AND on the script, I ahd no idea updating 0 rows would be considered OK.. I guess I can see why, but at the same time, sorta not.. using mysql_affected_rows works perfect Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 27, 2007 Share Posted July 27, 2007 Glad to hear it's working. You learn something new every day =) It would be a day wasted otherwise Quote Link to comment Share on other sites More sharing options...
per1os Posted July 27, 2007 Share Posted July 27, 2007 Checking if $result is false does not work. Reason being, an update statement can update 0 rows and it is still a valid SQL query. AHH! I didn't know but it all makes sense now!! that's why the query worked fine in phpmyadmin AND on the script, I ahd no idea updating 0 rows would be considered OK.. I guess I can see why, but at the same time, sorta not.. using mysql_affected_rows works perfect Think of it this way. If you have an if/else condition and you check if I is equal to one. But I is equal to 2, just because I does not equal 1 does not mean there is an error. Basically MySQL will only report an error if there is an error. That SQL Update statement is valid, but since none of the conditions were met no updating was done. No errors were encountered is the key here. Quote Link to comment Share on other sites More sharing options...
dj-kenpo Posted July 27, 2007 Author Share Posted July 27, 2007 Ya, that makes sense now, thanks guys! I love how everytime I learn soemthign I realize there's 1000's of things I don't know about php. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.