0

I have the following code:

// Create Connection
$mysqli = new mysqli($host, $username, $password, $db_name);

//Check connection
if($mysqli->connect_error){
    die("Connection failed: " . $mysqli->connect_error);
}
echo "Connected Successfully";



// Retrieve data from database 
$result= $mysqli->query("SELECT * FROM scores ORDER BY score DESC LIMIT 10");

Basically I need to know how to handle this data now. In the database I have a format:

Column 1: Name
Column 2: HighScore

I'm trying to display this on a webpage in a table so I need the query in a format which I can handle, what I have so far is:

// Start looping rows in mysql database.
while($rows=mysqli_fetch_array($result)){
    echo $rows['name'] . "|" . $rows['score'] . "|";

    // close while loop 
}

However, I don't believe I am using the mysqli_fetch_array() correctly as it isn't entering the while loop.

Qirel
  • 25,449
  • 7
  • 45
  • 62
Reece Hewitson
  • 91
  • 1
  • 10
  • 1
    Hi, if your column name is 'Name' you have to write $row['Name']. It's the same for score, if column name is 'HighScore' you have to write $row['HighScore'] – Sfili_81 Oct 03 '18 at 13:19
  • 1
    A more suitable duplicate would be http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index. – Qirel Oct 03 '18 at 13:30
  • 1
    @Qirel Added that also – RiggsFolly Oct 03 '18 at 13:33

2 Answers2

6

Fieldnames returned from this function are case-sensitive.

while($rows=mysqli_fetch_array($result)){
    echo $rows['Name'] . "|" . $rows['HighScore'] . "|";
    }
Rp9
  • 1,955
  • 2
  • 23
  • 31
0

As you using mysqli. So to fetch reuslt as in your case use as object oriented style:

$result= $mysqli->query("SELECT * FROM scores ORDER BY score DESC LIMIT 10");

while($rows=$result->fetch_assoc()){
  echo $rows['name'] . "|" . $rows['score'] . "|";
}

so not procedural style this:

$rows=mysqli_fetch_array($result)
somsgod
  • 357
  • 2
  • 11