0

I can fetch the a score from a database, but for some reason I keep getting an eerror: Notice: Undefined index: score in /var/www/html/game.php on line 26

PHP file where the game is:

<html>
<title>Breakout!</title>
<style>
@import url('https://fonts.googleapis.com/css?family=Press+Start+2P');
</style>
<?php include "db.php"; ?>
<link rel="icon" href="http://i.imgur.com/UJ9CaVW.png">
<head>
    <style>{ padding: 0; margin: 0; } canvas { background: #eee; display: block; margin: 0 auto; }</style>
</head>
<?php include 'stylesheet.php'; ?>
<?php include 'navbar.php'; ?>
<?php include 'opennavdiv.php' ?>
<body>
<div id="countdown"></div>
<!-- <audio src="Nintendo - Switch.mp3" autoplay="" loop=""></audio> -->
<div class="gamecon">
<canvas id="myCanvas" width="960" height="640"></canvas>
<script src='game.js' type="text/javascript"></script>
<?php include 'closenavdiv.php' ?>
</div>
<table>
    <tr>
        <td>Rank</td>
        <tr>
          <?php $score = DB::leaderboard($_GET["score"]); ?>
          <?php echo $score['score']; ?>
        </tr>
        <td>Name</td>
        <td>Score</td>
    </tr>
  </table>
</body>

</html>

Leaderboard function on my db.php file:

public static function leaderboard() {
    $connection = DB::CreateConnection();
    $rawResults = $connection->query("SELECT name, score FROM scores ORDER BY score DESC");
    $name = $rawResults->fetch_assoc();
    $score = $rawResults->fetch_assoc();
    return $name;
    return $score;
    $rank = 1;
    if (mysql_num_rows($result)) {
        while ($row = mysql_fetch_assoc($result)) {
            echo "<td>{$rank}</td>
                  <td>{$row['name']}</td>
                  <td>{$row['score']}</td>";
            $rank++;
        }
    }
  }
Shadow
  • 33,525
  • 10
  • 51
  • 64
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Irvin Dec 05 '16 at 00:23
  • A couple things, after a `return` the rest of the function will not be executed. So, you only return `$name`. The further parts in that function are not executed. Then, you are recieving an array where there is no such key as `score`. It's probably something like this: `$score[0]['score']` – Nytrix Dec 05 '16 at 00:32
  • Thanks for your help! What should I change to $score[0]['score'] – Matt Titone Dec 05 '16 at 01:04

0 Answers0