note 46704 added to ref.mysql
| From: | php at 3wheel dot net | Date: | Wed, 20 Oct 2004 17:42:29 +0000 |
| Subject: | note 46704 added to ref.mysql | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-78846@lists.php.net to get a copy of this message | ||
Re: Melvin Nava
The first select query is not needed. You can use mysql_affected_rows() to see if the update
modified any rows. Also, it is a waste to fetch a value and do the addition in PHP when you can do
it in the database (much safer). If no rows were affected, the insert is necessary:
<?
$c_update = "UPDATE stats_pagecounter SET page_hits = page_hits + 1 WHERE page_name =
'$page'";
$c_hit = mysql_query($c_update, $c_link)
or die('counter UPDATE error: '.mysql_errno().', '.mysql_error());
if (mysql_affected_rows($c_link) == 0) { //not found
$c_insert = "INSERT INTO stats_pagecounter
VALUES ( '0', '$page', '1')";
$c_page = mysql_query($c_insert, $c_link)
or die('counter INSERT error: '.mysql_errno().', '.mysql_error());
$pcounter = 1;
}
?>
----
Manual Page -- http://www.php.net/manual/en/ref.mysql.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+46704
Delete -- http://master.php.net/manage/user-notes.php?action=delete+46704&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46704&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46704&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46704&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46704&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46704&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+46704&report=yes
Search -- http://master.php.net/manage/user-notes.php