Results 1 to 6 of 6

Thread: me gettng replication in my DB :(

  1. #1

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    Did you solve this or just accidently erase both posts?
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  2. #2
    Fanatic Member ubunreal69's Avatar
    Join Date
    Apr 2001
    Location
    Morayfield, Australia
    Posts
    609
    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 ?

  3. #3

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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.

  4. #4

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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.
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  5. #5
    Fanatic Member ubunreal69's Avatar
    Join Date
    Apr 2001
    Location
    Morayfield, Australia
    Posts
    609
    thanx CP, i'll look over that code and give it a try

  6. #6
    Fanatic Member ubunreal69's Avatar
    Join Date
    Apr 2001
    Location
    Morayfield, Australia
    Posts
    609
    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
  •  



Click Here to Expand Forum to Full Width