Closed Thread Icon

Topic awaiting preservation: Issue with PHP and mySQL Counter Increment (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=22209" title="Pages that link to Topic awaiting preservation: Issue with PHP and mySQL Counter Increment (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Issue with PHP and mySQL Counter Increment <span class="small">(Page 1 of 1)</span>\

 
Hebedee
Paranoid (IV) Inmate

From: Maryland, USA
Insane since: Jan 2001

posted posted 06-16-2004 19:40
code:
<?php

if(isset($_COOKIE['user_ip'])) $set = true;

if(!$set)
{
setcookie("user_ip", $REMOTE_ADDR, time()+360000);
$link = mysql_connect($db_uri, $user_name, $pass)
or die("Could not connect: " . mysql_error());
mysql_select_db($user_name) or die("Could not select database" . mysql_error());
mysql_query("UPDATE simplecount SET count=(count + 1) WHERE count_id=1");
$inker++;
$whackerror = "this is an error used to help report bugs. please ignore it. - $inker";
}
else
{
$link = mysql_connect($db_uri, $user_name, $pass)
or die("Could not connect: " . mysql_error());
mysql_select_db($user_name) or die("Could not select database" . mysql_error());
}
?>


This code functions, in that it only adds values to the counter when the user does not have a cookie on them. However, when I run it without cookies, it increments by 2 (obviously providing a false count). Does anyone see anything wrong with this script? I have run it and printed the variable $inker and it only turns up one instance of the segment running, so it does not seem likely that the code itself is run twice. Any ideas?

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 06-17-2004 08:31

I see many things wrong with this simple piece of code.

Watch the magic unfold.

code:
<?php
//if(isset($_COOKIE['user_ip'])) $set = true; //completly unnessecary. And a potential bug, too

//repeated code should be written only once.
function error($strMessage)
{
print $strMessage. ': ';
print mysql_error(). ' - '. mysql_error();
die();
}

//common code should be common, not repetead in every instance.
$link = mysql_connect($db_uri, $user_name, $pass) or error("Could not connect"); //using the new error function.
mysql_select_db($user_name) or error("Could not select database"); //and again. We already saved something.

//if(!$set) //what happens if the user appends ?set=true to the url.
//we'll use this instead
if (isset($_COOKIES['user_ip']))
{
setcookie("user_ip", $REMOTE_ADDR, time()+360000); //I'd add a path and a domain.
$iCountID = 1; //we'll be using this magic value twice, so we pull it into a variable.
mysql_query("UPDATE simplecount SET count=(count + 1) WHERE count_id={$iCountID}") or error("update query failed");
//$inker++; //now, where does $inker come from. What's it's initial value that you're incrementing.
$strQuery = "SELECT count FROM simplecount WHERE count_id={$iCountID}";
$hRes = mysql_query($strQuery);
if ($hRes)
{
$arrRow = mysql_fetch_array($hRes);
if ($arrRow)
{
$inker = $arrRow['count'];
}
else
{
$inker = 0;
}
}
else
{
error("select query failed: $strQuery");
}

$whackerror = "this is an error used to help report bugs. please ignore it. - $inker"; //printing this might be a good idea.
}
//no else needed, since the case was now empty
?>



Now it looks better, and will be easier to extend.
Your actually might have wanted to set $inker = 0 at the beginning instead of pulling it out of the database, but you don't have any loops in there, it won't ever be run twice without calling the page twice (which might just be what's happening. Further investigation is needed).

So long,

->Tyberius Prime

Edit: With a closing [ code], it looks much better...

(Edited by Tyberius Prime on 06-17-2004 19:42)

Hebedee
Paranoid (IV) Inmate

From: Maryland, USA
Insane since: Jan 2001

posted posted 06-17-2004 17:35

This will be the third time I try to respond.


$inker was a far-fetched idea to try to see how many times that portion of code was run.

Database queries and connection openings have to come after cookies are sent or else errors about previously sent headers crop up.

I print $whackerror at the end to prevent aesthetic difficulties - this is, after all, a behind-the-scenes issue.

code:
if (!isset($_COOKIES['user_ip'])) 
{
setcookie("user_ip", $REMOTE_ADDR, time()+360000); //I'd add a path and a domain.
$update_count = true;
$whackerror = "this is an error used to help report bugs. please ignore it. - $inker";
}

$link = mysql_connect($db_uri, $user_name, $pass) or error("Could not connect");
mysql_select_db($user_name) or error("Could not select database");
$iCountID = 1;
$updatecount ? mysql_query("UPDATE simplecount SET count=(count + 1) WHERE count_id={$iCountID}") or error("update query failed") : $updatecount = false;



You'll see I condensed the code a bit. With this code, it functions properly. Thanks for the help.

Any suggestions?

(Edited by Hebedee on 06-17-2004 17:37)

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 06-17-2004 19:41

Rule 234:
*thou shall not use the tenary operator purly for side effects*

And that's the most important one. Use an if statement. Clearer. You'll understand it 6 months from now.
It isn't hidden by a tiny little '?'.

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 06-17-2004 20:08

еÐ3 wh47 4r3 J00 74£|{1n9 4b0µ7 4££ 7h3 £337 h4(|{3r$ µ$3 73rn4r¥ 0p3r470r$ ¥0µ w0µ£Ð r34£123 7h47 1 J00 w3r3 £337 700.

Hebedee
Paranoid (IV) Inmate

From: Maryland, USA
Insane since: Jan 2001

posted posted 06-17-2004 22:39

Alright. I was just experimenting with it.

It's changed.

(Edited by Hebedee on 06-17-2004 22:42)

« BackwardsOnwards »

Show Forum Drop Down Menu