-2

I wrote a script and it is not fetching data, I can't see anything wrong with it.

My script is:

<?php 
    $conn = mysqli_connect("*****", "*****", "*****", "*****");

    $sql = "SELECT * FROM points ORDER BY points DESC LIMIT 3;";
    $result = mysqli_query($conn, $sql);
    $resultCheck = mysqli_num_rows($result);

    if ($resultCheck > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            echo $row['players'] . "<br>"; 
        }
    }
?>
slavoo
  • 5,798
  • 64
  • 37
  • 39
joe
  • 7
  • 2
    You don't know what's wrong because you don't check for errors in your code. Never assume the code is always going to work flawlessly. Use [`mysqli_error()`](http://php.net/manual/en/mysqli.error.php) to get a detailed error message from the database. – John Conde Jan 23 '18 at 20:57
  • 2
    error reporting and `mysqli_error($conn)` will tell you. – Funk Forty Niner Jan 23 '18 at 20:57
  • 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 Jan 23 '18 at 21:13
  • 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 Jan 23 '18 at 21:13
  • Note: Try and get out of the habit of declaring SQL statements in throw-away variables that are used only once. It's a lot easier to follow code where the query is supplied directly to the function, and there's no longer a chance of messing up and sending in `$sql3` instead of the visually similar `$sql8`. – tadman Jan 23 '18 at 21:13

1 Answers1

0

Your code for fetching data from the database is working fine, You should check your database name or table name or a field name of the table. And it may happen that the field name of the table you trying to fetch does not exist in your table.

<?php 

$conn = mysqli_connect('localhost','root','','stackoverflow');

$sql = "SELECT * FROM task ORDER BY task_id DESC LIMIT 3 ;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);

if ($resultCheck > 0) 
{
    while ($row = mysqli_fetch_assoc($result)) {
        echo $row['license'] . "<br>"; 
    }
}

?>