Jump to content

Mysql....


marklarah

Recommended Posts

<?
$resulta = mysql_query("SELECT * FROM `Score` ORDER BY `Score` DESC LIMIT 5");
while ($row = mysql_fetch_array($resulta)){
$playerid = $row['userid'];
$resulta = mysql_query("SELECT * FROM `users` WHERE `userid` = '$playerid'");
$bob = mysql_fetch_array($resulta);
$player = $bob['username'];

echo '<b>'.$player.'</b>: '.$row['Score'].'<br>';

}

?>

 

I just get the first row of the highest player. I have to do this because it only inserts the user id's into the DB, and the ids with the names are in another table. So hewp.

 

Maybe a function?

Link to comment
https://forums.phpfreaks.com/topic/95290-mysql/
Share on other sites

Are you asking how to combine these queries? If so...you can do it like this:

 

<?php
$resulta = mysql_query("SELECT s.userid, u.username 
                        FROM Score s
                        LEFT JOIN users u
                        ON u.userid=s.userid
                        ORDER BY SCORE DESC
                        LIMIT 5");

while ($row = mysql_fetch_array($resulta)){   
   echo '<b>'.$row['username'].'</b>: '.$row['Score'].'<br>';
}

?>

 

If thats not what you want, explain a little more.

Link to comment
https://forums.phpfreaks.com/topic/95290-mysql/#findComment-488057
Share on other sites

hmm

Notice: Undefined index: Score in /home/ammostar/public_html/boards/scores.php on line 117

Robtcee13:

 

Notice: Undefined index: Score in /home/ammostar/public_html/boards/scores.php on line 117

Robtcee13:

 

Notice: Undefined index: Score in /home/ammostar/public_html/boards/scores.php on line 117

Robtcee13:

 

Notice: Undefined index: Score in /home/ammostar/public_html/boards/scores.php on line 117

Robtcee13:

 

Notice: Undefined index: Score in /home/ammostar/public_html/boards/scores.php on line 117

Robtcee13:

 

Not sure here...

 

Link to comment
https://forums.phpfreaks.com/topic/95290-mysql/#findComment-488065
Share on other sites

Oops, I forgot to select "Score" from the table in the query. Change it to this:

 

<?php
$resulta = mysql_query("SELECT s.userid, s.score, u.username 
                        FROM Score s
                        LEFT JOIN users u
                        ON u.userid=s.userid
                        ORDER BY SCORE DESC
                        LIMIT 5");

while ($row = mysql_fetch_array($resulta)){   
   echo '<b>'.$row['username'].'</b>: '.$row['Score'].'<br>';
}

?>

Link to comment
https://forums.phpfreaks.com/topic/95290-mysql/#findComment-488068
Share on other sites

I guess I'll just have to spell it for you then as you be bothered to even try it  ::)

 

This code fails when $row['Score'] is used

 

<?php
error_reporting (E_ALL);
include 'db.php';

/** THE TABLE **********************************
        DROP TABLE IF EXISTS `test`.`tbl_score`;
        CREATE TABLE `tbl_score` (
          `Player` varchar(40) default NULL,
          `Score` int(10) unsigned default NULL
        ) 
***********************************************/

$sql = "SELECT s.player, s.score
        FROM tbl_score s";
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res))
{
    echo $row['player'], ' ', $row['Score'], '<br />';
}
?>

Gives -->
Brak 
Notice: Undefined index: Score in C:\Inetpub\wwwroot\test\noname5.php on line 18

IronMaiden 
Notice: Undefined index: Score in C:\Inetpub\wwwroot\test\noname5.php on line 18

NINJA 
Notice: Undefined index: Score in C:\Inetpub\wwwroot\test\noname5.php on line 18

Reaper 
Notice: Undefined index: Score in C:\Inetpub\wwwroot\test\noname5.php on line 18

 

Whereas this works because "s.score" as used in the query is Lowercase even though the column in the table is "Score"

 

<?php
error_reporting (E_ALL);
include 'db.php';

/** THE TABLE **********************************
        DROP TABLE IF EXISTS `test`.`tbl_score`;
        CREATE TABLE `tbl_score` (
          `Player` varchar(40) default NULL,
          `Score` int(10) unsigned default NULL
        ) 
***********************************************/

$sql = "SELECT s.player, s.score
        FROM tbl_score s";
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res))
{
    echo $row['player'], ' ', $row['score'], '<br />';  // NOTE : Lcase score to match the colname from the query
}
?>

Gives -->
Brak 8850
IronMaiden 99250
NINJA 95457
Reaper 70100

Link to comment
https://forums.phpfreaks.com/topic/95290-mysql/#findComment-488738
Share on other sites

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.