Jump to content

[SOLVED] what am I doing wrong?


dj-kenpo

Recommended Posts

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 =)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.