|
-
Jul 3rd, 2002, 07:06 AM
#1
Thread Starter
Fanatic Member
Did you solve this or just accidently erase both posts?
-
Jul 3rd, 2002, 07:37 AM
#2
Fanatic Member
no, both posts just stuffed up, and i diidnt have time at the time to fix it all up.
the problem still tansds as follows: a mate of mine asked me to write some code that would count the amount of times a user has clicked a certain link. and also, to keep him from having to maintain the system, it should automatically create entities for pages that done already exist in the database (to store the info). this is the code i use to check whether the page already exists in the database or not, but it seems to be randomly duplicating data.
PHP Code:
// Get row count from table containing the display information
$table_rows = mysql_result(mysql_query("select count(*) from page_displays"), 0, "count(*)");
settype($table_rows, "integer");
$y = 0;
// Check is page exists in the database
While ($y < $table_rows)
{
$db_page = mysql_result(mysql_query("select page from page_displays"), $y, "page");
IF ($db_page == $page)
{$page_found = "1";}
ELSE
{$page_found = "0";}
$y++;
}
settype($page_found, "bool");
// The two different actions for whether a page does or doesnt exist in the database
IF ($page_found == 1)
{mysql_query("update page_displays set hits=page_displays.hits + 1 where page='$page'");}
IF ($page_found == 0)
{mysql_query("insert into page_displays (hits,page) values ('1','$page')");}
is there anything visibly wrong with my code that would make it duplicate data. or do i need to explain a bit more ?
-
Jul 3rd, 2002, 07:47 AM
#3
Thread Starter
Fanatic Member
Personally you are making this too complicated.
Try this:
PHP Code:
<?php
$found = 0;
$query = mysql_query("select * from page_displays");
for ($i=0;$i<mysql_numrows($query);$i++) {
if ($db_page == mysql_result($query,$i,"page"))
$found = 1;
}
if ($found == 1) {
mysql_query("update page_displays set hits=page_displays.hits + 1 where page='$db_page'");
} else {
mysql_query("insert into page_displays (hits,page) values ('1','$db_page')");
}
?>
Last edited by cpradio; Jul 3rd, 2002 at 08:05 AM.
-
Jul 3rd, 2002, 07:54 AM
#4
Thread Starter
Fanatic Member
Actually if you want to be even more effiecent use this:
PHP Code:
<?php
// checks to see how many records are returned.
// If 0 are returned then a record should be made.
if (mysql_numrows(mysql_query("select * from page_displays where page='$db_page'")) > 0) {
// page was found, update row
mysql_query("update page_displays set hits=page_displays.hits + 1 where page='$db_page'");
} else {
// page was not found, create row
mysql_query("insert into page_displays (hits,page) values ('1','$db_page')");
}
?>
Last edited by cpradio; Jul 3rd, 2002 at 08:16 AM.
-
Jul 3rd, 2002, 08:01 AM
#5
Fanatic Member
thanx CP, i'll look over that code and give it a try
-
Jul 4th, 2002, 06:24 PM
#6
Fanatic Member
that didnt fix it. I think its MySQL's fault, cos it seems to crash and **** lately, so I'm busy fixing that up @ the moment (its a pain in the ass)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|