0

I am trying to write a code to extract data from my MySQL database to a table on my website, I managed to setup a connection because it doesn't give me the failed to connect error, however the echo is just showing me the lines of code I wrote. I must have done something wrong.

The code I have:

<?php
$con=mysqli_connect("IP","username","password","db name");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  } 

$sql = "SELECT DISTINCT lastDisplayName FROM ranks ORDER BY `points` DESC     LIMIT 10";
$result = $con->query($sql);

if ($result->num_rows > 0) 
  {
// output data of each row
while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - Name: " . $row["lastDisplayName"]. " " .$row["points"]. "<br>";
  }
} else {
echo "0 results";
}
mysqli_close($con);
?>

I am a beginner and I made this code by viewing several online tutorials.

Edit: I dont really think someone has the exact same code with the exact same mistake. Thereby I do not think this question is a duplicate.

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Merty123
  • 1
  • 1
  • If you can see your own code your server is not working because it is not recognizing the PHP code. Install wamp, for example. – Jose Manuel Abarca Rodríguez Nov 04 '16 at 19:18
  • So, if I understand you correctly, you only get `id: - Name: ` and no data from the query? You actually have no error-checking on that query, which is bad... – junkfoodjunkie Nov 04 '16 at 19:18
  • what echo, the entire page of php text? – Drew Nov 04 '16 at 19:21
  • query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "id: " . $row["id"]. " - Name: " . $row["lastDisplayName"]. " " .$row["points"]. "
    "; } } else { echo "0 results"; } mysqli_close($con); ?>
    – Merty123 Nov 05 '16 at 01:36

0 Answers0