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
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
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
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
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
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.