0

I made this to display data but nothing appeared, and it shows no errors in coding

is there anyone who can help me

<?php
require 'db_connect.php';
$sql_get = "SELECT * FROM daftarumkm ORDER BY id_daftar DESC";
$query = $con->query($sql_get);
$response_data = null;
while ($data = $query->fetch_assoc()) {
    $response_data[] = $data;
}
if (is_null($response_data)) {
        $status = true;
} else {
    $status = true;
}

and this is db_connect.php

<?php
define('DB_USER', "root"); // db user
define('DB_PASSWORD', ""); // db password (mention your db password here)
define('DB_DATABASE', "data_umkm"); // database name
define('DB_SERVER', "localhost"); // db server

$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASSWORD,DB_DATABASE);

// Check connection
if(mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

?>
Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

-1

Your code for getting data from the database is fine, but there are some other issues.

1) If you want to check whether you got the results from database in the variable $response_data you have made a mistake here

if (is_null($response_data)) {
        $status = true;
} else {
    $status = true;
}

You probably want to change it to

if (is_null($response_data)) {
        $status = false;
} else {
    $status = true;
}

and I should mention thet there is a better way to do this. Instead of 4 lines above, you can simply write $status = (is_null($response_data)) ? false : true;

2) Furthermore, instead of all of the lines

$response_data = null;
while ($data = $query->fetch_assoc()) {
    $response_data[] = $data;
}
if (is_null($response_data)) {
        $status = true;
} else {
    $status = true;
}

you can write the following more efficient code:

$response_data = $query->fetch_all(MYSQLI_ASSOC);
$status = ($response_data) ? false : true;

3) At the end, if you want to see the results, simply use the var_dumb() function.

var_dumb($response_data);
MathCoder
  • 442
  • 3
  • 9