在显示MySQL数据时需要php / html的建议

时间:2016-03-25 21:41:26

标签: php html

所以我创建了这个脚本来测试我是否可以从我的数据库输出。它在php中运行良好,但是当我尝试在HTML中的div标签内提取该数据时,它不起作用。任何建议,谢谢。

//Connection to database, working fine, top of page
<?php
require "include/mysql.php";
if (!$con) {
die('Could not connect: ' . mysql_error());
}
?>

//This is also working as intended
<?php 
$sql = "SELECT * FROM `LISTINGS` ORDER BY `LISTINGS`.`YA` DESC";
$myData = mysql_query($sql,$con);
while($record = mysql_fetch_array($myData)){

    echo $record['TITLE'];

}
?>

//Here is were the problem, in the H2 tag, i'm trying to output a title but it is not working. 
<div class="cards-row">

    <div class="card-row">
        <div class="card-row-inner">
            <div class="card-row-image" data-background-image="assets/img/tmp/product-1.jpg">


            </div><!-- /.card-row-image -->

            <div class="card-row-body">
                <h2 class="card-row-title"><?php echo $record['TITLE'];?>title</h2>
                <div class="card-row-content">Description</div><!-- /.card-row-content -->
            </div><!-- /.card-row-body -->

            <div class="card-row-properties">
                <dl>

                        <dd>Test</dd><dt>Visit Website</dt>
                        <dd>Test</dd><dt>More Info</dt>                  
                        <dd>Added</dd><dt>AddedDate</dt>
                        <dd>Visited</dd><dt>Visited</dt>

                </dl>
            </div><!-- /.card-row-properties -->
        </div><!-- /.card-row-inner -->
    </div><!-- /.card-row -->


</div><!-- /.cards-row -->

我知道有很多未完成的代码,但我现在只是测试一些东西,抱歉语法不好。

2 个答案:

答案 0 :(得分:3)

您无法在while循环之外访问$record。如果您只想访问一条记录,只需摆脱循环并自行编写$record = mysql_fetch_array($myData);

答案 1 :(得分:2)

如果有很多行在while循环中移动html:

<?php 
$sql = "SELECT * FROM `LISTINGS` ORDER BY `LISTINGS`.`YA` DESC";
$myData = mysql_query($sql,$con);
while($record = mysql_fetch_array($myData)){
?>



<div class="cards-row">

<div class="card-row">
    <div class="card-row-inner">
        <div class="card-row-image" data-background-image="assets/img/tmp/product-1.jpg">


        </div><!-- /.card-row-image -->

        <div class="card-row-body">
            <h2 class="card-row-title"><?php echo $record['TITLE'];?>title</h2>
            <div class="card-row-content">Description</div><!-- /.card-row-content -->
        </div><!-- /.card-row-body -->

        <div class="card-row-properties">
            <dl>

                    <dd>Test</dd><dt>Visit Website</dt>
                    <dd>Test</dd><dt>More Info</dt>                  
                    <dd>Added</dd><dt>AddedDate</dt>
                    <dd>Visited</dd><dt>Visited</dt>

            </dl>
        </div><!-- /.card-row-properties -->
    </div><!-- /.card-row-inner -->
</div><!-- /.card-row -->
<?php 

}
?>