tiago peres tiago peres - 4 years ago 73
MySQL Question

Debug PHP code with if, while, arrays for showing data [solved]

I'm creating some code to display all the records from a MySQL database into a PHP view.

The query is perfect: using HeidiSQL it retrieves all the values needed:

see here

At the moment, is giving the following error:

error

Any help with some debug?

Muchas gracias

The code:

<?php $sql = "select * from specials group by Start_Date DESC";
$result = @mysqli_query($sql)
if($result){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo '<div class='deal-item col-md-12'>
<div class='col-md-4'>
<img src='lascruces_styles/img/deals-img/oil-deal.jpg' alt='' class='deal-thumb'>
<p class='expire'>The deal expires '.$row['End_Date'].'</p>
</div>
<div class='col-md-6 info-container'>
<h2 class='deal-title'>'.$row['Special_Name'].'</h2>
<p class='offer-user'>Offered by
<a href=''>'.$row['Added_By'].'</a></p>
<p class='deal-desc'>'.$row['Description'].'</p>
<div class='share-row'>
<a href='' class='share'>Share this deal</a>
<div class='social'>
<i class='icon-facebook'></i>
<i class='icon-gplus'></i>
<i class='icon-linkedin'></i>
<i class='icon-mail-squared'></i>
</div>
</div>
</div>
<div class='col-md-2 view-deal-container'>
<p class='old-price'>'.$row['Normal_Price'].'</p>
<p class='current-price'>'.$row['Special_Price'].'</p>
<a href=''><div class='view-deal'>
<p>VIEW DEAL</p>
</div>
</a>
</div>
</div>';
mysqli_free_result ($result); // Free up the resources }
}
?>

Answer Source

You're missing a couple of things.The main things are that you're missing the connection parameter in your mysqli_query() call, and your single quotes are messing all your syntax. Try in this manner:

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "select * from specials group by Start_Date DESC";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while ($row = mysqli_fetch_assoc($result)) {
        echo '<div class="deal-item col-md-12">
                    <div class="col-md-4">
                        <img src="lascruces_styles/img/deals-img/oil-deal.jpg" alt="" class="deal-thumb">
                        <p class="expire">The deal expires ' . $row['End_Date'] . '</p>
                    </div>
               </div>';
    }
} else {
    echo "0 results";
}

mysqli_close($conn);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download