Re: Increase field value by 1 problem
| From: | Richard Lynch | Date: | Wed, 22 Aug 2001 00:25:34 +0000 |
| Subject: | Re: Increase field value by 1 problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-63823@lists.php.net to get a copy of this message | ||
> function updateHitCounter($id,$PHPSESSID,$type){
> $flag=$this->validateSession($PHPSESSID);
> if($flag==1){
> include("admin/config.inc.php");
> $connection =
> mysql_connect("$host","$username","$password");
> if ($connection == false){
> echo mysql_errno().": ".mysql_error()."<BR>";
> exit;
> }
> if($type=="out") $query = "select * from sites where
> id='$id'";
> elseif($type=="in") $query = "select * from sites where
> id='$id'";
# It's the same query?...
> $result = mysql_db_query ("$dbase", $query);
> if ($result){
> $numOfRows = mysql_num_rows ($result);
> $hitsIn= mysql_result ($result, $i, "hitsOut");
> $hitsOut= mysql_result ($result, $i, "hitsIn");
I'm guessing one of these is line 222, and you have no record for that '$id'
yet in your database.
For a brand new site, you'll need to register them first, or stick an @ in
front of these, or...
Or, just use your $numOfRows to decide if there's a value to retrieve or
not.
> }else{
> echo mysql_errno().": ".mysql_error()."<BR>";
> }
> if($type=="out"){
> $hits=$hitsOut;
> }else{
> $hits=$hitsIn;
> }
> $hits++;
> $query = "UPDATE sites SET hitsOut ='$hits' WHERE
> id='$id'";
And this has no effect, since there are no rows with that id of '$id'
> $result = mysql_db_query ("$dbase", $query);
> if (!$result){
> echo mysql_errno().": ".mysql_error()."<BR>";
> }
> mysql_close ();
> }
> }
> When I run it i get this error:
> Warning: Unable to jump to row 0 on MySQL result index 4 in c:\program
> files\apache group\apache\htdocs\phptopsites\top100.php on line 222
> Guys is there anyway i can avoid it?
Unless there's more to this function you are not showing, you could just do:
function updateHitCounter($id,$PHPSESSID,$type){
$flag=$this->validateSession($PHPSESSID);
if($flag==1){
include("admin/config.inc.php");
$connection = mysql_connect("$host","$username","$password");
if ($connection == false){
echo mysql_errno().": ".mysql_error()."<BR>";
exit;
}
$query = "update sites set $type = $type + 1 where id = '$id'";
$update = mysql_query($query);
}
}
You could then use http://php.net/mysql_affected_rows to find out if
there
was a site you even updated...
--
WARNING richard@zend.com address is an endangered species -- Use
ceo@l-i-e.com
Wanna help me out? Like Music? Buy a CD: http://l-i-e.com/artists.htm
Volunteer a little time: http://chatmusic.com/volunteer.htm