0

i create displaydata but i want condition like below

function display(){
    $get_data   = "SELECT * FROM table";
    $run_data   = mysqli_query($connect, $get_data);
    $check_data = mysqli_fetch_array($run_data);

                if($check_data == 0){
                    echo "No post yet";
                }
                else
                {
                    echo "Display post";
                }
}

the data did not display "no post yet" when the query is none.

did i do wrong ?

thanks in advance.

Rzj Hayabusa
  • 592
  • 1
  • 8
  • 27

2 Answers2

2

You could likely use mysqli_num_rows and process accordingly like:

function display(){
    global $connect;

    $sql = "SELECT * FROM table";

    $result = mysqli_query( $connect, $sql );

    if( $result && mysqli_num_rows( $result ) > 0 ){

        while( $rs = mysqli_fetch_array( $result ) ){
            echo $rs['id'],$rs['title'];/*etc*/
        }
        mysqli_free_result( $result );

    } else {
        echo 'No post yet';
    }
}
Professor Abronsius
  • 30,177
  • 5
  • 29
  • 43
-1

you can use :-

function display(){
$get_data   = "SELECT * FROM table";
$run_data   = mysqli_query($connect, $get_data);
$check_data = mysqli_fetch_array($run_data);
$count=count($check_data);
            if($count == 0){
                echo "No post yet";
            }
            else
            {
                echo "Display post";
            }

}

or you can use :-

function display(){
$get_data   = "SELECT * FROM table";
$run_data   = mysqli_query($connect, $get_data);
if(mysqli_num_rows($run_data))
{
 while($check_data = mysqli_fetch_array($run_data))
 {
     print_r($check_data);
 }
}
else
{
    echo "No post yet";
}   

}