使用if,while,数组调试PHP代码以显示数据

时间:2016-12-23 13:13:50

标签: php mysql

我正在创建一些代码,以便将MySQL数据库中的所有记录显示为PHP视图。

查询非常完美:使用HeidiSQL可以检索所需的所有值:

see here

目前,发出以下错误:

error

对某些调试有何帮助?

Muchas gracias

代码:

    <?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                        } 
                }
            ?>

2 个答案:

答案 0 :(得分:1)

你遗漏了一些东西。主要的是你在mysqli_query()调用中缺少连接参数,你的单引号会弄乱你的所有语法。试试这种方式:

$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);

答案 1 :(得分:0)

您没有正确引用字符串,这会导致语法错误。您还要将mysql_*mysqli_*混合在一起。

我假设您正在使用的MySQLi连接在此处被称为$mysqli,请相应更改(或者如果您使用其他API,则需要适应使用它, API不混合。

正确引用它应该看起来像这样。

<?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  
}
?>