[PHP] Wrong Results [+REP]
#1

Hello,

Since I am not so experienced with PHP I faced a problem with ranking players on forums, I have this php code

Код:
						<?php
						$query = $mysqlcon->prepare("SELECT playerName, playerMoney FROM `playerdata` ORDER BY playerMoney DESC LIMIT 0,10");
						
						$query->execute();
						if($query->rowCount() == 0)
						{
							echo "<tr><td colspan='6'><small>There are currently no registered players.</small></td></tr>";
						}
						while($data = $query->fetch())
						{							
							echo '<tr><td>
							<a href="profile.php?user='.$data['playerName'].'">'.$data['playerName'].'</a>
							</td>';

							echo "<td>".$data['playerMoney']."</td></tr>";
						}
						?>
But it just prints random players, can't really understand what is the problem, if you could help me solve this out I would be very grateful, thanks for reading

Output
Reply
#2

It looks like your `playerMoney` table is a varchar. So the data ordered lexically.
If it so, you can use convert() function from mysql itself, or by editing your `playerMoney` table to int.
Example of using convert() :
Код:
$query = $mysqlcon->prepare("SELECT playerName, playerMoney FROM `playerdata` ORDER BY CONVERT(playerMoney, INTEGER) DESC LIMIT 0,10");
Reply
#3

Quote:
Originally Posted by X337
Посмотреть сообщение
It looks like your `playerMoney` table is a varchar. So the data ordered lexically.
If it so, you can use convert() function from mysql itself, or by editing your `playerMoney` table to int.
Example of using convert() :
Код:
$query = $mysqlcon->prepare("SELECT playerName, playerMoney FROM `playerdata` ORDER BY CONVERT(playerMoney, INTEGER) DESC LIMIT 0,10");
I get the following results now

Код:
There are currently no registered players.
Reply
#4

Quote:
Originally Posted by SecretBoss
Посмотреть сообщение
I get the following results now

Код:
There are currently no registered players.
Sorry, missed something in the query.
This query should be work :
Код:
$query = $mysqlcon->prepare("SELECT playerName, playerMoney FROM `playerdata` ORDER BY CONVERT(playerMoney, SIGNED INTEGER) DESC LIMIT 0,10");
Reply
#5

Quote:
Originally Posted by X337
Посмотреть сообщение
Sorry, missed something in the query.
This query should be work :
Код:
$query = $mysqlcon->prepare("SELECT playerName, playerMoney FROM `playerdata` ORDER BY CONVERT(playerMoney, SIGNED INTEGER) DESC LIMIT 0,10");
Now it just keeps loading

EDIT

Wrong, I wrote sth wrong, works like a dime thanks a lot mate
Reply
#6

Next time would I suggest you to do something like,
PHP код:
<?php
                        $query 
$mysqlcon->prepare("SELECT * FROM `playerdata` ORDER BY playerMoney DESC LIMIT 0,10");
                        
                        
$query->execute();
                        if(
$query->rowCount() == 0)
                        {
                            echo 
"<tr><td colspan='6'><small>There are currently no registered players.</small></td></tr>";
                        }
                        while(
$data $query->fetch())
                        {                            
                            echo 
'<tr><td>
                            <a href="profile.php?user='
.$data['playerName'].'">'.$data['playerName'].'</a>
                            </td>'
;
                            echo 
"<td>".$data['playerMoney']."</td></tr>";
                        }
                        
?>
Reply
#7

Quote:
Originally Posted by Meller
Посмотреть сообщение
Next time would I suggest you to do something like,
PHP код:
<?php
                        $query 
$mysqlcon->prepare("SELECT * FROM `playerdata` ORDER BY playerMoney DESC LIMIT 0,10");
                        
                        
$query->execute();
                        if(
$query->rowCount() == 0)
                        {
                            echo 
"<tr><td colspan='6'><small>There are currently no registered players.</small></td></tr>";
                        }
                        while(
$data $query->fetch())
                        {                            
                            echo 
'<tr><td>
                            <a href="profile.php?user='
.$data['playerName'].'">'.$data['playerName'].'</a>
                            </td>'
;
                            echo 
"<td>".$data['playerMoney']."</td></tr>";
                        }
                        
?>
I can't see any differences
Reply
#8

At the query, I used *
That will result taking out every field names, And then echo them by their rows name.
Reply
#9

Quote:
Originally Posted by Meller
Посмотреть сообщение
At the query, I used *
That will result taking out every field names, And then echo them by their rows name.
This is not what I want, also there will be no rows
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)