0

I've spent the past 2 hours trying to solve this one error. I am a complete rookie so I dont know what's going on. Here's the code, please help:

<?php
header('Access-Control-Allow-Origin: *');




$host="localhost"; // Host name 
$username="id11111_ab"; // Mysql username 
$password="*****"; // Mysql password 
$db_name="id11111_cd"; // Database name 
$tbl_name="ef"; // Table name

// Connect to server and select database.
$link = mysqli_connect($host, $username, $password, $db_name);

// Retrieve data from database 
$sql = "SELECT * FROM scores ORDER BY score DESC LIMIT 10";
$result = mysqli_query($link,$sql);

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

// close while loop 
}
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Rusty Spoons
  • 9
  • 1
  • 2
  • What is the value of var_dump($link) – Mohammad Ahmad Jul 09 '18 at 16:11
  • If your query result is a boolean, then your query failed. You need to check for errors to see why – Don't Panic Jul 09 '18 at 16:15
  • Im sorry but I have no idea. I was following a guide to make a Global HighScore setup and he used this code – Rusty Spoons Jul 09 '18 at 16:16
  • 3
    Check out this Q&A to see how to set up error reporting: https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-information-in-different-environments – Don't Panic Jul 09 '18 at 16:17
  • Note: The object-oriented interface to `mysqli` is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface. Before you get too invested in the procedural style it’s worth switching over. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is an artifact from the PHP 4 era when `mysqli` API was introduced and should not be used in new code. – tadman Jul 09 '18 at 16:51
  • 1
    This question is practically Stack Overflow's national anthem. I'm amazed that people can create them given how the titles have to be unique. – tadman Jul 09 '18 at 16:52
  • A lot of problems can be detected and resolved by [enabling exceptions in `mysqli`](https://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli) so mistakes aren't easily ignored. – tadman Jul 09 '18 at 16:52
  • 1
    @tadman the line number varies ;) – Don't Panic Jul 09 '18 at 19:13

2 Answers2

0

mysqli_query() returns false if it fails. Subsequently, the mysqli_fetch_array() function is being passed this false boolean value, on which it can't operate. You'd be wise to check the value that mysqli_query() returns isn't false prior to attempting to retrieve the resource. E.g.:

$result = mysqli_query($link,$sql);
if (!$result) {
    die('Query failed');
}
Ewan
  • 181
  • 8
  • Ok, i get the gist of it. how do i proceed than? – Rusty Spoons Jul 09 '18 at 16:23
  • I'd suggest you check your query and database. Make sure the data is present, the field names are correct, the auth info is accurate, the connection is being established, find out why the error is occurring. Setup error reporting as mentioned in one of the comments above. Find out why the query fails and then fix it. – Ewan Jul 09 '18 at 16:25
  • It responded 'Query failed' – Rusty Spoons Jul 09 '18 at 16:31
0

It seems like the mysql connection is not established properly. Check for errors using this:

if(mysqli_connect_errno()){
echo mysqli_connect_error();
}
M. Verma
  • 1
  • 2
  • I don't think this is the case. If the connection failed using the procedural `mysqli_connect`, then `$link` would be `false` and they would get an error when they tried to run `mysqli_query`. – Don't Panic Jul 09 '18 at 16:41
  • The query failed, not the connection. – tadman Jul 09 '18 at 16:51