Submit Your Article Forum Rules

Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Counter driving me nuts

  1. #1
    Senior Member
    Join Date
    Nov 2003
    Posts
    108

    Counter driving me nuts

    Ok, heres my script (PHP) im trying to use to make a counter (gets count from database, adds 1, puts that back in database, and shows the number) but, i have an error somewhere (I get a blank page) but i cant find it, any help?
    <?php
    $user = "user";
    $host = "host";
    $password = "password";
    $database = "database";
    $connection = mysql_connect($host,$user,$password) or die ("Couldn't connect to server");
    $db = mysql_select_db($database,$connection)
    or die ("Couldn't select database");
    $query = "SELECT Counter FROM Counted";
    $result = mysql_query($query,$connection)
    or die("Couldn't execute query.");

    $Counter = +1;

    echo $Counter;

    $query = "SELECT Counter FROM Counted";
    $query = "UPDATE Counted WHERE Counter = '$Counter'";
    or die ("Couldn't execute other query.");
    ?>

  2. #2
    Senior Member splinter's Avatar
    Join Date
    Jul 2003
    Posts
    118
    $query = "UPDATE Counted WHERE Counter = '$Counter'";
    or die ("Couldn't execute other query.");
    You put a ; after '$Counter'"

    That shouldn't be there. I hate it when that happens :P I always forget to put ; after I've echo'd something.

    echo "Damn! I missed it out"

    Grrrrrrrrrrrr.

  3. #3
    Senior Member mushroom's Avatar
    Join Date
    Feb 2004
    Posts
    286
    I see many things that confuse me in your script the main error I see is that $counter is never innialized.
    With out knowing the struture of you table it is difficut to write a script for it but try the following script.

    <?php
    $user = "user";
    $host = "host";
    $password = "password";
    $database = "database";
    $connection = mysql_pconnect($host,$user,$password) or die ("Couldn't connect to server");

    mysql_select_db($database) or die ("Couldn't select database");

    $query = "SELECT Counter FROM Counted";
    $result = mysql_query($query)
    or die("Couldn't execute query.");
    while ($row = mysql_fetch_array ($result))
    {$counter=$row[0];}

    $Counter++;

    echo $Counter;


    $mysql_query("UPDATE Counted SET Counter = '$Counter'")
    or die ("Couldn't execute other query.");
    ?>
    Irony: That for most people the most "trusted" web site on the planet is for a company the has been convicted of criminal activity.

    Both Security and SuSe start with "S". www.eemam.com

  4. #4
    Senior Member mushroom's Avatar
    Join Date
    Feb 2004
    Posts
    286
    Sorry minnor typo.

    {$counter=$row[0];}
    should be
    {$Counter=$row[0];}
    Irony: That for most people the most "trusted" web site on the planet is for a company the has been convicted of criminal activity.

    Both Security and SuSe start with "S". www.eemam.com

  5. #5
    Senior Member httpman's Avatar
    Join Date
    Aug 2003
    Posts
    121
    From my point of vue, after you have sent your SQL request you don't read the returned values. So your $Counter will always be undefined.

    You should add this to your code (in red) :

    ----
    $result = mysql_query($query,$connection)
    or die("Couldn't execute query.");

    $line=mysql_fetch_array($result);
    $Counter=$line['Counter'];


    $Counter = +1;
    ...
    ----

    (assuming that your SQL request returned only 1 line of datas from your table, and that your field with the counter is called "Counter" within that same table)

    Jean-Pierre
    www.net-createurs.com [ french only website sorry ! ]

  6. #6
    Senior Member
    Join Date
    Feb 2004
    Posts
    157
    Running off of what you posted, there are a couple of things that don't quite work...
    (my comments // correction are in bold)

    <?php
    $user = "user";
    $host = "host";
    $password = "password";
    $database = "database";
    $connection = mysql_connect($host,$user,$password) or die ("Couldn't connect to server");
    $db = mysql_select_db($database,$connection) or die ("Couldn't select database");
    $query = "SELECT Counter FROM Counted";
    $result = mysql_query($query,$connection) or die("Couldn't execute query.");

    $arrCounter = mysql_fetch_array($result);
    $Counter = arrCounter['Counter'];


    $Counter = +1;

    echo $Counter;

    $result = mysql_query("UPDATE Counter SET Counter='$Counter'") or die ("Couldn't execute other query.");
    ?>

  7. #7
    Senior Member
    Join Date
    Jul 2003
    Posts
    398
    Being PHP eliterate:

    Does

    $Counter = +1;

    increment $Counter? I'd guess it should be

    $Counter += 1;

    Beware the first line though!

    K<o>

  8. #8
    Senior Member
    Join Date
    Feb 2004
    Posts
    157
    Good eye. :) I totally missed that.

  9. #9
    Senior Member
    Join Date
    Jul 2003
    Posts
    398
    Quote Originally Posted by Dawson
    Good eye. :) I totally missed that.
    Huh! I don't know why I dared to write that. Lucky me it was correct. I just have programmed in so many languages, that I'm ashamed to tell people how many. So I gave it a shot.

    K<o>

  10. #10
    Senior Member
    Join Date
    Feb 2004
    Posts
    157
    $counter++; is even easier, too. :)

Page 1 of 2 12 LastLast

Similar Threads

  1. Driving traffic driving me crazy *Help*
    By mhooper2 in forum Google Discussion Forum
    Replies: 7
    Last Post: 09-25-2008, 11:02 AM
  2. eTrust EZ Antivirus - Driving Me Nuts!
    By BaldyBob in forum IT Discussion Forum
    Replies: 2
    Last Post: 06-06-2006, 09:31 PM
  3. Yahoo going nuts? or is it Me?
    By Asiana in forum Yahoo! Discussion Forum
    Replies: 3
    Last Post: 11-17-2004, 06:30 AM
  4. remote files and hosts - It's Driving me nuts!
    By lsblogs in forum Web Programming Discussion Forum
    Replies: 0
    Last Post: 09-23-2004, 07:18 PM
  5. HELP! Bizarre page problem is driving me nuts!
    By minstrel in forum Graphics & Design Discussion Forum
    Replies: 24
    Last Post: 10-26-2003, 10:18 AM

Posting Permissions

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