SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Feb 2001
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have a table that has two columns, urls and hits.
    Just want to display the hits for a particular url. Below is how far have managed to get. Newbie so please make allowances

    <?

    $hostname = "localhost";
    $username = "mine";
    $password = "mine";
    $userstable = "mine";
    $dbName = mine";

    /* make connection to database */
    MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect to database");

    @mysql_select_db( "$dbName") or die( "Unable to select database");


    $query = "SELECT urls,hits FROM $userstable WHERE urls = 'url'";

    $result = MYSQL_QUERY($query);

    $hits = MYSQL_NUMROWS($result);

    $i = 0;

    IF ($number == 0) :
    PRINT "<CENTER><P>No downloads yet!</CENTER>";
    ELSEIF ($number > 0) :
    PRINT "<CENTER><P>Downloads: $hits<BR><BR>";
    WHILE ($i < $number):
    $urls = mysql_result($result,$i,"urls");
    $hits= mysql_result($result,$i,"hits");
    PRINT "hits: $number.";
    PRINT "<BR><BR>";
    $i++;
    ENDWHILE;
    PRINT "</CENTER>";
    ENDIF;
    ?>

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do you update the database to record a new hit? I'm assuming that you increment the value of hits in the relevent url record. Also, I suggest that in your database you define hits as NOT NULL DEFAULT 0

    Then your query can look like this:
    $query = "SELECT hits FROM $userstable WHERE urls = '$url'";

    Now to work out how many hits you have for $url
    $hits = mysql_fetch_field($result, 0);

    Your result set should only have one row and one column (thus the offset of 0) which will contain the value for hits.

  3. #3
    SitePoint Member
    Join Date
    Feb 2001
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Table consists of urls and hits. Each time a file is downloaded,
    hits is increased by 1. If file has never been downloaded before, full url of file is entered into urls column, and hits would then show 1.

    "Also, I suggest that in your database you define hits as NOT NULL DEFAULT 0 "

    Tried to do this but access denied. However, altered my php to the following.Would this be correct if I set up another table,
    exactly as before, but configured hits as NOT NULL DEFAULT 0?

    <?

    /* declare some relevant variables */
    $hostname = "localhost";
    $username = "mine";
    $password = "mine";
    $userstable = "mine";
    $dbName = "mine";

    /* make connection to database */
    MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect to database");

    @mysql_select_db( "$dbName") or die( "Unable to select database");

    $query = "SELECT hits FROM $userstable WHERE urls = '$url";

    $hits = mysql_fetch_field($result, 0);

    $i = 0;

    IF ($number == 0) :
    PRINT "<CENTER><P>No downloads yet!</CENTER>";
    ELSEIF ($number > 0) :
    PRINT "<CENTER><P>Downloads: $hits<BR><BR>";
    WHILE ($i < $number):
    $hits= mysql_result($result,$i,"hits");
    PRINT "hits: $number.";
    PRINT "<BR><BR>";
    $i++;
    ENDWHILE;
    PRINT "</CENTER>";
    ENDIF;
    ?>

    Thank you for taking time to help/advise

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK when you create your table, use "DEFAULT 0 NOT NULL" - that's the right way around (if in fact it makes a difference).
    Code:
    CREATE TABLE tableName (
       url TEXT,
       hits INT UNSIGNED DEFAULT 0 NOT NULL,
       PRIMARY KEY (url) )
    Here's how I would code it. Because you know that the default value for count is 0, if there are no hits you count = 0
    PHP Code:
    // open a database connection as per your code
    $query "SELECT hits FROM $usertable WHERE urls = '$url'";
    $result mysql_query($query);
    $hitsArr mysql_fetch_row($result);
    $hits $hitsArr[0];
    if (
    $hits != 0) {
       print 
    "The number of hits for $url is $hits"
    } else {
       print 
    "No hits for this url";

    The above code (and the preceding create table sql) assumes that there is only one row per url in the database. However, you have a loop in your code suggesting that there is more than one row per url in your database? In this case you need a primary key other than url. I'm not sure I understand your data structure. Any way, if there can be multiple records for the same url then you can loop through the result set like so
    PHP Code:
    $query "SELECT hits FROM $usertable WHERE urls = '$url'";
    $result mysql_query($query);
    while ( 
    $hitsArr mysql_fetch_row($result) ) {
       
    $hits $hitsArr[0];
       if (
    $hits != ) {
          
    // do your printing here
       
    } else {
          
    // print no hits here
       
    }

    Last edited by freakysid; Apr 9, 2001 at 12:03.

  5. #5
    SitePoint Member
    Join Date
    Feb 2001
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Parse error: parse error in fred4.php on line 19

    Is the error I get on your above suggestion.(BTW each url
    only has one row)

    The database has to be configured as below or does not work at all.

    CREATE TABLE name
    urls varchar(100) NOT NULL,
    hits int(10) DEFAULT '1' NOT NULL,
    PRIMARY KEY (urls)

  6. #6
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh yes, that create query looks better - I forgot field size and single quotes around the default value. Also, the code wasn't dispaying properly in my previous reply. I fixed it up.

    You have a parse error. That means there is a syntax error - usually a missing } or ) or ; or ,

    There was a missing semicolon on this line:
    $hits = $hitsArr[0];


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
  •