1

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

How can I debug this?

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                        } 
                }
            ?>
halfer
  • 19,824
  • 17
  • 99
  • 186
Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145

2 Answers2

1

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);
CodeGodie
  • 12,116
  • 6
  • 37
  • 66
0

You're not quoting your string properly, which is causing syntax errors. You're also mixing mysql_* with mysqli_* here.

I'm assuming the MySQLi connection you're using is called $mysqli here, change it accordingly (or if you're using another API, you need to adapt to use that, APIs don't mix).

Properly quoted it should look something like this.

<?php 
$sql = "select * from specials group by Start_Date DESC";
if ($result = mysqli_query($mysqli, $sql)) {
    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  
}
?>
Tolios
  • 219
  • 3
  • 12