0

I'm looking for a way to check if my mysqli_query result is empty.

My code:

$result = mysqli_query($con, "SELECT ......");

Then there is the table head:

<table class="table table-striped" width="100%">
<thead>
<table class="table table-striped" width="100%">
        <thead>
            <th>Jahr</th>

And then I generate the rows with a while loop:

while($ausgabe = mysqli_fetch_object($result)){ ?>
        <tr>
            <td><?php echo $ausgabe->jahr; ?></td>

And now I want to make an if query to check if I have to display the table, before the table begins, something like this:

if($result){    
<table class="table table-striped" width="100%">
        <thead>
            <th>Jahr</th>

But this does not work, it only works with asking if mysqli_fetch_object is empty but this code is below my table head so I can't use this.

Do you have any ideas?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
dida110
  • 111
  • 1
  • 9
  • LOOK AT THE MANUAL if you need to find out whats available [`$result->num_rows`](http://php.net/manual/en/mysqli-result.num-rows.php) – RiggsFolly Apr 30 '18 at 17:08
  • How much [research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – RiggsFolly Apr 30 '18 at 17:11
  • The manual is useless for me I already checked it otherwise I would't have written this! If I use num_rows in front of my mysqli_fetch_object then my mysqli_fetch_object is empty after... the only way to make it work for me is to make two mysqli_query but this seems inefficient for me. – dida110 Apr 30 '18 at 17:34

1 Answers1

2

You can use the $num_rows

$results = $mysqli->query("SELECT * FROM table");
if($results->num_rows === 0)
{
   echo 'No results';
 }
else
{
//HERE YOU PROCESS 
while(){....}
}
Diego Avila
  • 700
  • 7
  • 24
  • This is not working for me, I tried it with if(mysqli_num_rows($result)===0){ but it's alway empty. If I remove the num_rows there is my output again... – dida110 Apr 30 '18 at 17:26