[MySQL + PHP] Top 10 Players
#1

Hello!

I'm trying to make a top 10 list based on the kills a player has made but I just don't seem to make it work,so basically when i reload the page nothing shows up on it.
I'm a beginner at MySQL and PHP so I am asking for your help here.
This is the code I came up with (Note that I am not experienced with MySQL):
PHP Code:
<?php
$handle 
= new PDO('mysql:dbname=rgamersc_minigames;host=localhost''database''password');
$results mysql_query ('SELECT Username FROM player_accounts ORDER BY Kills DESC LIMIT 10');
    while (
$row mysql_fetch_array($results))
{
   echo 
$row['Username'];
   echo 
"shit";
}
?>
Reply
#2

Since you are using PDO, you have to use the PDO functions and not mysql (http://php.net/manual/en/book.pdo.php).
Also the mysql_ functions are deprecated and you should use mysqli instead in PHP.

Example of your code in PDO:
PHP Code:
<?php 
$handle 
= new PDO('mysql:dbname=rgamersc_minigames;host=localhost''database''password');
foreach (
$handle->query('SELECT Username FROM player_accounts ORDER BY Kills DESC LIMIT 10') AS $row) {
    echo 
$row['Username'];
}
?>
Reply
#3

That makes sense.
Meanwhile,I managed to get it working along with a friend and it looks pretty neat for 2 rookies.
If anyone is interested in the code feel free to PM me.

Click for DEMO
Reply
#4

Quote:
Originally Posted by BlackBank3
View Post
Since you are using PDO, you have to use the PDO functions and not mysql (http://php.net/manual/en/book.pdo.php).
Also the mysql_ functions are deprecated and you should use mysqli instead in PHP.

Example of your code in PDO:
PHP Code:
<?php 
$handle 
= new PDO('mysql:dbname=rgamersc_minigames;host=localhost''database''password');
foreach (
$handle->query('SELECT Username FROM player_accounts ORDER BY Kills DESC LIMIT 10') AS $row) {
    echo 
$row['Username'];
}
?>
First you tell him to use PDO then you tell him to use mysqli? You are aware they are two completely different extensions.

@OP Use PDO
Reply
#5

Hello there,

For anyone interested in how this can be done:

connection.php:
PHP Code:
<?php
    $database_host 
"localhost";
    
$database_user "root";
    
$database_pass "";
    
$database_db "database_name";
    
$connection = new PDO("mysql:host=$database_host;dbname=$database_db;charset=utf8"$database_user$database_pass);
    
$connection -> setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
?>
top_kills.php:
PHP Code:
<?php
    
include_once 'connection.php';
    
$query $connection -> prepare("SELECT `name`, `kills` FROM `accounts` ORDER BY `kills` DESC LIMIT 15");
    if(
$query -> execute())
    {
        
$row_count $query -> rowCount();
        if(
$row_count)
        {
            while(
$query_result $query -> fetch())
            {
                echo 
$query_result['name'] . ': ' $query_result['kills'] . '<br>';
            }
        }
    }
?>
Reply
#6

Quote:
Originally Posted by Aldo.
View Post
First you tell him to use PDO then you tell him to use mysqli? You are aware they are two completely different extensions.

@OP Use PDO
It's okay,I've decided to give it a try using mysqli and does the trick. I was wondering if there's anyone here that knows how to make the stats update automatically without the need of refreshing the page?
Reply
#7

Quote:
Originally Posted by [rG]Cold
View Post
It's okay,I've decided to give it a try using mysqli and does the trick. I was wondering if there's anyone here that knows how to make the stats update automatically without the need of refreshing the page?
Then you would need to update player stats in game every now and then. If the page is only implemented to show top ten stats, I do not recommend to refresh page automatically. MySQLi: <http://php.net/manual/en/book.mysqli.php>.
Reply
#8

Quote:
Originally Posted by [rG]Cold
View Post
It's okay,I've decided to give it a try using mysqli and does the trick. I was wondering if there's anyone here that knows how to make the stats update automatically without the need of refreshing the page?
ajax and jquery
Reply
#9

How exactly,Kyle?
Reply
#10

Quote:
Originally Posted by KevinReinke
View Post
Hello there,

For anyone interested in how this can be done:

connection.php:
PHP Code:
.... 
top_kills.php:
PHP Code:
<?php
    
include_once 'connection.php';
    
$query $connection -> prepare("SELECT `name`, `kills` FROM `accounts` ORDER BY `kills` DESC LIMIT 15");
    if(
$query -> execute())
    {
        
$row_count $query -> rowCount();
        if(
$row_count)
        {
            while(
$query_result $query -> fetch())
            {
                echo 
query_result['name'] . ': ' query_result['kills'] . '<br>';
            }
        }
    }
?>
if($row_count > 0) { ... }
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)