Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 5 of 5

Thread: PHP Mysql counter

  1. #1

    PHP Mysql counter

    Hi, I've got a counter that connects to a database.. But it's not working.. Can somebody please tell me why, or maybe you have another script for it :

    PHP Code:
    //part1
    $page=$_SERVER['PHP_SELF'];


    //part2

    include('db_connect.php');



    //part4
    $result mysql_query("SELECT * FROM counter WHERE page='$page'",$connnews);

    $sql="";
    $count=1;

    if(
    mysql_num_rows($result)==0)//we don't have a record for it
    {

    //part5
    $sql=<<<SQL
    INSERT INTO counter VALUES(
    '
    $page',
    1
    )
    SQL;

    }
    else if(
    mysql_num_rows($result)>0)
    {

    //part6
    $sql=<<<SQL
    UPDATE counter SET count=count+1 WHERE page='$page'
    SQL;
    $array=mysql_fetch_assoc($result);

    $count=$array['count'];

    }


    //part7
    $newResult mysql_query($sql,$connnews);


    //part8
    print $count+1;

    mysql_close($connnews); 
    thanks

  2. #2
    You're not saying what it's not doing but i'll have a go. I think its:
    '$page'
    Variables in php in between single qoutes are not evaluated to variables.....

    so you'll have to write "'". $page . "'"

  3. #3
    ^wrong. variables in single quotes aren't evaluated to variables unless those single quotes are also wrapped in double quotes. in that case, the single quotes are taken literally and the variable is evaulated. so pucca's syntax is correct.

    my first suggestion is to echo out your variables prior to using them. especially $page prior to your first query.

  4. #4

    Thanks. I got another script now, that IS actually working, on my localhost.. But on the server it doesn't.. How's that possible??

    PHP Code:
    //make a connection
    include('db_connect.php');
    //store the result of the query in $result
    $result mysql_query("SELECT * FROM counter");
    //retrieve the fields in our table
    $fields mysql_fetch_row($result);
    //update the count field by 1
    mysql_query("UPDATE counter SET count = count+1");
    //$mycount variable is set to the first field (count)
    $mycount $fields[0];
    //display the count
    echo $mycount
    I've got another file pointing to the db_connect.php, so it's not as if my connection is wrong.. Is it maybe something to do with that InnoDB / MyISAM stuff??? Cause, on my db on my localhost, it's MyISAM, and on the remote server, it's the other one? But I doubt it, huh?

    Thanks!

  5. #5
    never mind. I just had to insert 1...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012