[MySQL + PHP] Top 10 Players -
[rG]Cold - 05.05.2016
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";
}
?>
Re: [MySQL + PHP] Top 10 Players -
BlackBank - 05.05.2016
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'];
}
?>
Re: [MySQL + PHP] Top 10 Players -
[rG]Cold - 05.05.2016
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
Re: [MySQL + PHP] Top 10 Players -
Aldo. - 06.05.2016
Quote:
Originally Posted by BlackBank3
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
Re: [MySQL + PHP] Top 10 Players -
KevinReinke - 06.05.2016
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_ERRMODE, PDO::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>';
}
}
}
?>
Re: [MySQL + PHP] Top 10 Players -
[rG]Cold - 06.05.2016
Quote:
Originally Posted by Aldo.
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?
Re: [MySQL + PHP] Top 10 Players -
CrossUSAAF - 06.05.2016
Quote:
Originally Posted by [rG]Cold
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>.
Re: [MySQL + PHP] Top 10 Players -
Kyle - 06.05.2016
Quote:
Originally Posted by [rG]Cold
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
Re: [MySQL + PHP] Top 10 Players -
[rG]Cold - 06.05.2016
How exactly,Kyle?
Re: [MySQL + PHP] Top 10 Players -
iZN - 06.05.2016
Quote:
Originally Posted by KevinReinke
Hello there,
For anyone interested in how this can be done:
connection.php:
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) { ... }