Derbyshire10 Derbyshire10 - 4 months ago 13
MySQL Question

If statement in sql result

Is there a way I will be able to get the if function to work For the end date? So if the end date is blank it will display present.

The if statement is only needed to make the end date of the current version being made look better if it has no date and looks like 00/00/0000

I receive error 500 from this however without the if function, it works just as it should.

Full code:

<!DOCTYPE html>
<html lang="en-GB">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="robots" content="noindex">
<title>Test</title>
<style>
.row {
margin: 10px;
}
@media screen and (max-width: 543px) {
#date {
display: none;
}
}
</style>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" integrity="sha384-y3tfxAZXuh4HwSYylfB+J125MxIs6mR5FOHamPBG064zB+AFeWH94NdvaCBm8qnd" crossorigin="anonymous">
</head>
<body align="center">
<h1>Test</h1>
<div class="container">
<div class="row">
<div class="col-xs-6 col-sm-3">
<p>Version:</p>
</div>
<div class="col-xs-6 col-sm-3">
<p>Description</p>
</div>
<div class="col-xs-6 col-sm-3" id="date">
<p>Start Date:</p>
</div>
<div class="col-xs-6 col-sm-3" id="date">
<p>Finish Date:</p>
</div>
</div>
<hr>
<?php
$servername = "<--enter servername -->";
$username = "<--enter username -->";
$password = "<--enter password -->";
$dbname = "<--enter datbase name -->";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, Description, Start_date, End_date FROM Version ORDER BY ID DESC";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<div class='row'>
<div class='col-xs-6 col-sm-3'>
<p><a href='v". $row["id"]."/'>Version ". $row["id"]."</a></p>
</div>
<div class='col-xs-6 col-sm-3'>
<p>". $row["Description"]."</p>
</div>
<div class='col-xs-6 col-sm-3' id='date'>
<p>". $row["Start_date"]."</p>
</div>
<div class='col-xs-6 col-sm-3' id='date'>
<p>" if(. $row["End_date"]. == 00/00/0000):
echo "Present";
else:
echo . $row["End_date"].;
endif;"</p>
</div>
</div><hr>";
}
} else {
echo "0 results";
}

$conn->close();
?>







Answer

Lots of mistakes here I am afraid

if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<div class='row'>
               <div class='col-xs-6 col-sm-3'>
                <p><a href='v". $row["id"]."/'>Version ". $row["id"]."</a></p>
               </div>
               <div class='col-xs-6 col-sm-3'>
                <p>". $row["Description"]."</p>
               </div>
               <div class='col-xs-6 col-sm-3' id='date'>
                <p>". $row["Start_date"]."</p>
               </div>
               <div class='col-xs-6 col-sm-3' id='date'>
            <p>";
        if( $row['End_date'] == '00/00/0000'):
            echo "Present";
        else:
            echo $row["End_date"];
        endif;

        echo "</p>
              </div>
              </div><hr>";
     }
} else {
    echo "0 results";
}

Assuming the null date is a standard MYSQL date format you might find that you need to test for

if( $row['End_date'] == '0000/00/00'):

instead of the '00/00/0000' date layout you have used