koshi
koshi

Reputation: 33

How to check there is data, then show nothing

I want to check if the data is empty. Is it's empty don't show enything even not the google link. If there is data then show the google link. How can I fix it?

$result = $mysqli->query("SELECT * FROM teams WHERE teamid = ".$_GET['teamid']." ORDER BY `teamname` DESC");

   $teamdetails = mysqli_fetch_assoc($result);

echo '<table id=kalender_table><tr><td><h3>'.$teamdetails['teamname'].'</h3> <br>';
echo ''.$teamdetails['teamid'].'<br>';
echo '<a href="'.$teamdetails['website'].'" target="_blank">'.$teamdetails['website'].'</a> <br></td>';
echo '<td><img src=../../logo/'.$teamdetails['image'].'></td></tr>';
echo '<tr><td colspan="2">'.$teamdetails['cmp1_name'].'</td></tr>';
echo '<tr><td colspan="2">'.$teamdetails['cmp1_adress'].'</td></tr>';
echo '<tr><td colspan="2">'.$teamdetails['cmp1_zip'].' '.$teamdetails['cmp1_city'].'</td></tr>';
echo '<tr><td colspan="2">'.$teamdetails['cmp1_phone'].'</td></tr>';
echo '<tr><td colspan="2"><a href="https://www.google.be/maps/place/'.$teamdetails['cmp1_adress'].'+'.$teamdetails['cmp1_zip'].'+'.$teamdetails['cmp1_city'].'" target="_blank">Google maps</a></td></tr>';

Upvotes: 2

Views: 50

Answers (4)

Shehary
Shehary

Reputation: 9992

Simple do it with row counts, if greater then 0 show if not, nothing to show

$result = $mysqli->query("SELECT * FROM teams WHERE teamid = ".$_GET['teamid']." ORDER BY `teamname` DESC");
$teamdetails = mysqli_fetch_assoc($result);

if((mysqli_num_rows($result) > 0) {
    echo '<table id=kalender_table><tr><td><h3>'.$teamdetails['teamname'].'</h3> <br>';
    echo ''.$teamdetails['teamid'].'<br>';
    echo '<a href="'.$teamdetails['website'].'" target="_blank">'.$teamdetails['website'].'</a> <br></td>';
    echo '<td><img src=../../logo/'.$teamdetails['image'].'></td></tr>';
    echo '<tr><td colspan="2">'.$teamdetails['cmp1_name'].'</td></tr>';
    echo '<tr><td colspan="2">'.$teamdetails['cmp1_adress'].'</td></tr>';
    echo '<tr><td colspan="2">'.$teamdetails['cmp1_zip'].' '.$teamdetails['cmp1_city'].'</td></tr>';
    echo '<tr><td colspan="2">'.$teamdetails['cmp1_phone'].'</td></tr>';
    echo '<tr><td colspan="2"><a href="https://www.google.be/maps/place/'.$teamdetails['cmp1_adress'].'+'.$teamdetails['cmp1_zip'].'+'.$teamdetails['cmp1_city'].'" target="_blank">Google maps</a></td></tr>';
} else {
    echo '<tr><td colspan="2">'Nothing to Show'</td></tr>';
}

Upvotes: 1

Schien
Schien

Reputation: 3903

right after running mysqli_fetch_assoc:

if (mysqli_affected_rows()){
...
}

Upvotes: 0

Oscar Lidenbrock
Oscar Lidenbrock

Reputation: 816

you need to use "isset" function because using count() may exist rows but have no data in them.

[...]
$teamdetails = mysqli_fetch_assoc($result);

if (isset($teamdetails['teamname']) & $teamdetails['teamname']) {
  echo '<table id=kalender_table><tr><td><h3>'.$teamdetails['teamname'].'</h3> <br>';
  [...]
}
[...]

Upvotes: -1

koredalin
koredalin

Reputation: 445

You can try this:

<?php
if (is_array($teamdetails) && count($teamdetails) > 0) {
    // Do something
}

Upvotes: 1

Related Questions