0

I have been working on this php to grab data from my MySQL database but it is unable to show results it just prints

Our connection is ok! 0 results

There are records in the database and the query works if I run it in phpMyAdmin. But it is unable to read it

database

  <?php
  $server = 'localhost';
  $pass = 'Blah';
  $user = 'Blah';
  $dbName = 'Blah';

  $db = new mysqli($server, $user, $pass, $dbName);

  if ($db->ping()) {
      printf ("Our connection is ok!\n");
  } else {
      printf ("Error: %s\n", $db->error);
  }
  if ($_GET['t'] == 'PL'){
      $table = 'Planets';
  } elseif ($_GET['t'] == 'SY') {
      $table = 'Systems';
  }elseif ($_GET['t'] == 'ST'){
      $table = 'Stars';
  }  else{

  };

$sql = "SELECT * FROM Planets WHERE ID =3";

$results = $db->query($sql);
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo $row["ID"];
      };
} else {
    echo "0 results";
};

?>

Of course I have changed passwords and database details but they are correct in my version

SteamPunk_Devil
  • 169
  • 1
  • 8
  • 1
    It's a typo `$result` and error reporting would have told you about an `undefined variable result` notice. http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Apr 02 '16 at 15:59
  • 1
    At the top of your script - `error_reporting(E_ALL); ini_set('display_errors', 1);` (always when developing and testing code) – Michael Berkowski Apr 02 '16 at 16:01
  • 1
    Note that a terminating `;` after closing `}` is not needed or used in PHP except when defining functions as variables (which you're not doing). They're harmless, but unusual and unnecessary. – Michael Berkowski Apr 02 '16 at 16:02
  • @Fred-ii- Damn so it is. Thanks – SteamPunk_Devil Apr 02 '16 at 16:03
  • And if typo noted by @Fred does not help then add following after `$db = new mysqli` if that really can be connection problem `/* check connection */ if ($db ->connect_errno) { printf("Connect failed: %s\n", $db->connect_error); exit(); }` – mkungla Apr 02 '16 at 16:03
  • @SteamPunk_Devil You're welcome ;-) – Funk Forty Niner Apr 02 '16 at 16:07

0 Answers0