SA-MP Forums

Go Back   SA-MP Forums > Other > Everything and Nothing

Reply
 
Thread Tools Display Modes
Old 05/05/2016, 10:56 AM   #1
[rG]Cold
Huge Clucker
 
[rG]Cold's Avatar
 
Join Date: Aug 2012
Posts: 234
Reputation: 35
Default [MySQL + PHP] Top 10 Players

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";
}
?>
[rG]Cold is offline   Reply With Quote
Old 05/05/2016, 03:58 PM   #2
BlackBank
Gangsta
 
Join Date: Dec 2010
Location: The Netherlands
Posts: 523
Reputation: 437
Default Re: [MySQL + PHP] Top 10 Players

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'];
}
?>
BlackBank is online now   Reply With Quote
Old 05/05/2016, 08:58 PM   #3
[rG]Cold
Huge Clucker
 
[rG]Cold's Avatar
 
Join Date: Aug 2012
Posts: 234
Reputation: 35
Default Re: [MySQL + PHP] Top 10 Players

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
[rG]Cold is offline   Reply With Quote
Old 06/05/2016, 02:27 AM   #4
Aldo.
Huge Clucker
 
Aldo.'s Avatar
 
Join Date: Jan 2011
Posts: 404
Reputation: 83
Default Re: [MySQL + PHP] Top 10 Players

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
Aldo. is offline   Reply With Quote
Old 06/05/2016, 02:37 AM   #5
KevinReinke
Banned
 
Join Date: May 2016
Posts: 100
Reputation: 13
Default Re: [MySQL + PHP] Top 10 Players

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>';
            }
        }
    }
?>

Last edited by KevinReinke; 07/05/2016 at 09:28 PM. Reason: Forgot two $s.
KevinReinke is offline   Reply With Quote
Old 06/05/2016, 09:48 AM   #6
[rG]Cold
Huge Clucker
 
[rG]Cold's Avatar
 
Join Date: Aug 2012
Posts: 234
Reputation: 35
Default Re: [MySQL + PHP] Top 10 Players

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?
[rG]Cold is offline   Reply With Quote
Old 06/05/2016, 10:15 AM   #7
CrossUSAAF
Huge Clucker
 
CrossUSAAF's Avatar
 
Join Date: Jun 2012
Location: Finland
Posts: 476
Reputation: 38
Default Re: [MySQL + PHP] Top 10 Players

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>.
__________________
Quote:
Originally Posted by RyderX View Post
REMOVE THIS SORRY MY NET WAS GETTING SHOWER REMOVE THIS!
CrossUSAAF is offline   Reply With Quote
Old 06/05/2016, 06:45 PM   #8
Kyle
High-roller
 
Kyle's Avatar
 
Join Date: Mar 2009
Location: United Kingdom
Posts: 2,258
Reputation: 330
Default Re: [MySQL + PHP] Top 10 Players

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
__________________
Kyle is offline   Reply With Quote
Old 06/05/2016, 07:42 PM   #9
[rG]Cold
Huge Clucker
 
[rG]Cold's Avatar
 
Join Date: Aug 2012
Posts: 234
Reputation: 35
Default Re: [MySQL + PHP] Top 10 Players

How exactly,Kyle?
[rG]Cold is offline   Reply With Quote
Old 06/05/2016, 08:38 PM   #10
iZN
High-roller
 
Join Date: Jun 2010
Posts: 2,376
Reputation: 569
Default Re: [MySQL + PHP] Top 10 Players

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) { ... }
__________________
iZN is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[FilterScript] [FS] Top 10 Players MySQL lukitaxxx_samp Filterscripts 12 08/04/2014 06:03 PM
MySQL - Players Get Same Stats Camacorn Scripting Help 3 03/08/2012 10:52 AM
TOP 'X' Players SQL to MySQL - Help nGen.SoNNy Scripting Help 1 05/05/2012 09:01 AM
[MySQL] Top 10 Players maxi459 Scripting Help 2 30/10/2011 05:49 PM
TOP Players in MYSQL? Ricop522 Scripting Help 2 13/10/2011 03:42 AM


All times are GMT. The time now is 01:12 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.