Page 1 of 1

php/mysql - didn't mean to make new topic - still need help

Posted: Tue Oct 06, 2009 7:32 pm
by RustyDoorknobs
Thanks, I changed what you said. But it's still not working. I'll explain my full situation.

I have a php script that gets image data and then echoes it. That part works fine. But, i want to be able to track how many times the script is accessed, so everytime the users accesses the script, I insert another hit in the hits table. On the main homepages, I use a php function(count_num_rows) to count the number of rows in the 'hits' table. That parts looks like it works fine. Here is my entire code for the image script (with some info changed for security) :

Code: Select all

 
<?php
 
            $con = mysql_connect("localhost","db","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
function genRandomString() {
    $length = 10;
    $characters = "0123456789abcdefghijklmnopqrstuvwxyz";
    $string = "";
 
    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }
 
    return $string;
}
            $string = genRandomString();
 
            mysql_select_db("db", $con);
            $sql = "INSERT INTO `hits` VALUES ('3', '{$string}')";
            mysql_query($sql,$con);
 
 
if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
 
    
    // get bytearray
    $jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
 
    
 
 
    // add headers for download dialog-box:
        header('Content-Type: image/jpeg');
        header("Content-Disposition: attachment; filename=".$_GET['name']);
        
 
 
    echo $jpg;
    
 
            
 
 
}
 
 
 
?>
 
 
 
So yeah, and here is what I use to count the number of rows:

Code: Select all

 
 
    require("db_connect.php");
mysql_select_db("db", $con);
$result = mysql_query("SELECT * FROM hits", $con);
$num_rows = mysql_num_rows($result);
 
echo "$num_rows";
 
Any ideas?

Re: php/mysql - didn't mean to make new topic - still need help

Posted: Tue Oct 06, 2009 8:23 pm
by Weiry
RustyDoorknobs wrote:But it's still not working
Can you specify what is not working exactly? What are you expecting that you are not getting?

A couple of things to check first,
1. Is the entry being successfully inserted into the 'hits' table?
2. What result are you getting when your number of hits code is executed?

also, when you are selecting the number of hits, are you checking total hits, or individual hits per image?
If you are checking the individual hits per image, then this code will not return the proper number

Code: Select all

$result = mysql_query("SELECT * FROM hits", $con);
you would need to specify the ID number of the image being tracked like:

Code: Select all

"SELECT * FROM `hits` WHERE `id` = '{YOURIMAGENUMBER}'"