#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0

    Mysql Count Cant obtain result


    hi, i have zero php knowledge so please forgive my stupidity

    im trying to count the number of users in my database who have a score higher than a given score the query works fine when run through phpmyadmin but im having problems getting results from my php script

    the query - $query = "SELECT COUNT(*) FROM `scores` WHERE `score` > '$score'";

    and heres what i have so far
    <?php
    $db = mysql_connect('localhost', 'honeycom_hive', 'Asd_FG33') or die('Could not connect: ' . mysql_error());
    mysql_select_db('honeycom_hive_scores') or die('Could not select database');

    // Strings must be escaped to prevent SQL injection attack.
    $name = mysql_real_escape_string($_GET['name'], $db);
    $score = mysql_real_escape_string($_GET['score'], $db);
    $hash = $_GET['hash'];


    $secretKey="mysecretkey"; # Change this value to match the value stored in the client javascript below

    $real_hash = md5($name . $score . $secretKey);
    if($real_hash == $hash) {
    // Send variables for the MySQL database class.
    $query = "SELECT COUNT(*) FROM `scores` WHERE `score` > '$score'";
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());
    $num_results = mysql_num_rows($result);

    while ($row = mysql_fetch_assoc($result)) {
    echo $row[$score];

    }
    m //mysql_free_result($result);
    mysql_result($result, 0);
    }
    ?>

    if it helps im trying to get the result back into unity3d but believe my problem is in the php script, any help would be greatly appreciated
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    So, 'scores' is your table name AND column name we're working with here? Just making sure. Maybe try altering your statement as:
    PHP Code:
    $query "SELECT COUNT(*) FROM `scores` WHERE `score` > " $score
    You can even echo the statement to see if it prints out in a manner you are aiming for. And just a note as a few may toss in, in your learning of databases, try to no longer use the mysql_ items. MySQLi and PDO are the newer thing, and Prepared Statements for security. If you're still learning rather from the start, go PDO. And perhaps look into some OOP, just to toss something out there.

    EDIT: I just noted the scores/score. My bad.

    EDIT2: And I suppose I should ask. What is a score? i.e. The format held within that column. If a straight number, it won't need to be quoted within your statement. Otherwise, it may be.
    Last edited by Triple_Nothing; July 2nd, 2013 at 10:36 AM.
  4. #3
  5. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Also:

    - Be careful not to publicize login data you use. We don't know your server name or IP so knowing your database's username/password is of minimal value, but still... no point in sharing sensitive info.

    - Try to migrate away from mysql_* to either mysqli_* or PDO. mysql_* is deprecated as of PHP 5.5.

    - Referencing Triple's second Edit, since you aren't validating the input as numeric, keep the quotes. If a user passes in a non-numeric value the query won't function correctly, but at least it won't throw an error. Ideally you should validate the input, though.

IMN logo majestic logo threadwatch logo seochat tools logo