For Programmers: Free Programming Magazines  


Home > Archive > PHP SQL > April 2006 > Counter problem









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Counter problem
Duderino82

2006-04-06, 7:59 am

I wanted to ad a counter to records. So when the user views the details
of the record I punch in this php line:

$qry = mysql_query("SELECT * FROM $table WHERE code='$code'",$db);
$row=mysql_fetch_array($qry);
$row[view]++;
$str="UPDATE $table SET view='$row[view]' WHERE code=$code";
mysql_query($str,$db);

when I check $row[view] after the increment it actually cointains
$row[view]++. Even the SQL string is correct. Lets say $row[view] was
23 then $str is:

UPDATE $table SET view='24' WHERE code=$code

The problem is that in the database the value will be 25!!! 'view'
field is a basic INT (11) (no auto_increment). I tryed to put in the
same value I get (in che example 23) with the result that he doesn't
change the value (it remains 23). What is the problem? and I can I
solve it???

J.O. Aho

2006-04-06, 6:59 pm

Duderino82 wrote:
> I wanted to ad a counter to records. So when the user views the details
> of the record I punch in this php line:
>
> $qry = mysql_query("SELECT * FROM $table WHERE code='$code'",$db);
> $row=mysql_fetch_array($qry);
> $row[view]++;
> $str="UPDATE $table SET view='$row[view]' WHERE code=$code";
> mysql_query($str,$db);



> when I check $row[view] after the increment it actually cointains
> $row[view]++. Even the SQL string is correct. Lets say $row[view] was
> 23 then $str is:
>
> UPDATE $table SET view='24' WHERE code=$code


Try with

$str="UPDATE $table SET view=view+1 WHERE code=$code";


//Aho
Duderino82

2006-04-07, 3:59 am

nope...same problem...is there any function besisde auot_increment that
adds 1 to a specific field?

J.O. Aho

2006-04-07, 6:59 pm

Duderino82 wrote:
> nope...same problem...is there any function besisde auot_increment that
> adds 1 to a specific field?
>


If you have problems with

$str="UPDATE $table SET view=view+1 WHERE code=$code";

then you have two possible problems

1. Your php script is run twice

2. your column view has auot_increment enabled


To fix 1, you have to ensure that the script is only loaded once, if you
include the file with the code that counts up the view column, then use
include_once() instead of include().
http://www.php.net/manual/en/function.include-once.php

To fix 2, you will need to use ALTER and remove the auot_increment for the
column view. http://dev.mysql.com/doc/refman/5.0/en/alter-table.html


//Aho
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com