SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why won't this work???

    hi all,

    the below code is a simple count function. It is supposed to update the simplecount table each time the page is visited.

    However, the code gives the final result of 0 rows updated.

    Why is this?

    Thanks.
    PHP Code:
    <?php 
        
    echo 'hello'
             
            
    // Database connection 
            
    $db mysql_pconnect('localhost''user''pword'); 
             
            if (!
    $db
            { 
                echo 
    'Could not connect'
                exit; 
            } 
             
            
    mysql_select_db('test') or die ('No Database'); 
             
            
    $query "update simplecount set count = count+1"
            
    $result mysql_query($query) or die(mysql_error()); 
             
            if (
    $result
            { 
                echo 
    mysql_affected_rows().' inserted into DB'
            } 
             
             

    ?>

  2. #2
    Born to Code rainadaman's Avatar
    Join Date
    Feb 2005
    Location
    india
    Posts
    793
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    put echo after
    $query = "update simplecount set count = count+1";
    check wat it says
    nothing seems wrong to me,
    also check the DB names etc...
    I was born intelligent,
    But Google ruined me.

  3. #3
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No luck.

    Any ideas?

    My table has two fields:

    count int (default value = 0)
    count_id int (default value =1)

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    whats count_id?
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    count_id is set to one. Guess I could leave it out? But I tried omitting that field but made no difference.

  6. #6
    SitePoint Addict rokc's Avatar
    Join Date
    Oct 2004
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i am not sure abt this but isn't this query wrong
    PHP Code:
    $query "update simplecount set count = count+1"
    I mean u are incrementing the column name and not the column content..i dont mysql on my computer..but running the same query in oracle has given me an invalid column name error.

  7. #7
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't think so. What should my query be??

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    OK, I think my theory is sound so try this...
    PHP Code:
    $select mysql_query("select `count` from simplecount") or die(mysql_error());
    $r mysql_fetch_assoc($select);

    $count=$rcount;
    $count=$count++;

    $update=mysql_query("update simplecount set count='$count'); 
    Something like that!

    SpikeZ
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  9. #9
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Didn't work Spikez. Below is my code. Any ideas??


    PHP Code:
    // Database connection
    $db mysql_pconnect('localhost''username''pword');
     
    if (!
    $db)
    {
    echo 
    'Could not connect';
    exit;
    }
     
    mysql_select_db('test') or die ('No Database');
     
    $query "select count from simplecount";
    $result mysql_query($query) or die(mysql_error());
     
    $r mysql_fetch_assoc($select);
     
    $count=$rcount
    $count=$count++;
     
    $update=mysql_query("update simplecount set count='$count'"); 
     
    if (
    $update)
    {
    echo 
    mysql_affected_rows().' inserted into DB';


  10. #10
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    I'm an ejot!

    $count=$rcount;
    $count=$count++;

    should be...
    $count=$r['count'];
    $count=$count++;

    Sorry!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  11. #11
    Employed Again Viflux's Avatar
    Join Date
    May 2003
    Location
    London, On.
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $query "select count from simplecount";
    $result mysql_query($query) or die(mysql_error());
     
    $r mysql_fetch_assoc($select);
     
    $count=$r["count"]; 
     
    $update=mysql_query("update simplecount set count=" $count ); 

  12. #12
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there inverted commas missing at the end??

  13. #13
    Chessplayer kleineme's Avatar
    Join Date
    Apr 2004
    Location
    Germany
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    set "count = count + 1 " is correct, but do you already have at least one row in your table? If simplecount doesn't have any content, then there will be nothing which can be updated, and thus you will get the message that zero rows are affected. In this context your message "inserted into DB" is misleading as your are not inserting but updating something
    Never ascribe to malice,
    that which can be explained by incompetence.
    Your code should not look unmaintainable, just be that way.

  14. #14
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    Oklahoma City, OK, USA
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually:

    PHP Code:
    <?php
    // Database connection
    $db = @mysql_pconnect('localhost''username''pword') or die(mysql_errno.': '.mysql_error());

    @
    mysql_select_db('test') or die('No Database');

    $query 'SELECT `count` FROM `simplecount`';
    $result mysql_query($query) or die(mysql_errno.': '.mysql_error());

    $r mysql_fetch_assoc($result);

    $newcount $r['count'] + 1;

    $update mysql_query("UPDATE `simplecount` SET `count` = $newcount"); 

    if (
    $update)
    {
        echo 
    mysql_affected_rows().' inserted into DB';


    ?>

  15. #15
    SitePoint Wizard
    Join Date
    Apr 2004
    Location
    dublin
    Posts
    2,036
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers Mr Orange! That worked.

    How do I display the count stored in the database??

    Thanks everyone!!

  16. #16
    - Michael - Bytecon's Avatar
    Join Date
    Mar 2004
    Location
    Ireland
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Display the count -

    PHP Code:
    echo "Count = " $r['count'] . ""

  17. #17
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    Oklahoma City, OK, USA
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just store it in a variable like so:

    PHP Code:
    <?php
    // Database connection
    $db = @mysql_pconnect('localhost''username''pword') or die(mysql_errno.': '.mysql_error());

    @
    mysql_select_db('test') or die('No Database');

    $query 'SELECT `count` FROM `simplecount`';
    $result mysql_query($query) or die(mysql_errno.': '.mysql_error());

    $r mysql_fetch_assoc($result);
    $oldcount $r['count'];
    $newcount $oldcount 1;

    $update mysql_query("UPDATE `simplecount` SET `count` = $newcount");

    if (
    $update)
    {
        echo 
    mysql_affected_rows().' inserted into DB';
    }

    echo 
    $oldcount.' is the old count.';
    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •