1

我真的需要一些帮助。我正在尝试建立一个网站并从 mysql 数据库中提取内容。我要填充的 div 如下。我遇到的问题是如何将 php 代码合并到 div 中?

<div class="cardetails">
    <img src="img/carpic.jpg"/>
    <p class="make"><a href="car.html">'model of car'<a></p>
    <p class="price">£Price</p>
    <div class="clear"></div>
</div>


$db = mysql_connect("localhost", "root", "Password1")
    or die ("Couldn't connect to Server!");
$result = mysql_select_db("my_cars", $db)
    or die ("Couldn't select Database!");
$query = "select img, make, price, from tbl_automatic WHERE MakeID = '7'";
$result = mysql_query($query,$db)
    or die ("Couldn't execute query!");

提前谢谢大家

4

2 回答 2

2
<?php
$db = mysql_connect("localhost", "root", "Password1")
    or die ("Couldn't connect to Server!");
$result = mysql_select_db("my_cars", $db)
    or die ("Couldn't select Database!");
$query = "select img, make, price, from tbl_automatic WHERE MakeID = '7'";
$result = mysql_query($query,$db)
    or die ("Couldn't execute query!");

while($q = mysql_fetch_array($result)){
?>
<div class="cardetails">
    <img src="<?=$q['img']?>"/>
    <p class="make"><a href="car.html"><?=$q['make']?><a></p>
    <p class="price"><?=$q['price']?></p>
    <div class="clear"></div>
</div>

<?php
}
?>

我希望它能解决你的问题。

于 2013-10-05T18:54:29.897 回答
0

neeagl 打败了我,但无论如何这是我的。正如评论中提到的,现在最好使用 mysqli 或 pdo 。

<?php
$db = mysql_connect("localhost", "root", "Password1")
    or die ("Couldn't connect to Server!");
$result = mysql_select_db("my_cars", $db)
    or die ("Couldn't select Database!");
$query = "select img, make, price, from tbl_automatic WHERE MakeID = '7'";
$result = mysql_query($query,$db)
    or die ("Couldn't execute query!");


$cardetails = '';

while($row = mysql_fetch_array($result)) {
$cardetails .= '
<div class="cardetails">
    <img src="img/'.$row['img'].'"/>
    <p class="make"><a href="car.html">'.$row['make'].'<a></p>
    <p class="price">£'.$row['price'].'Price</p>
    <div class="clear"></div>
</div>';
}

echo $cardetails;
于 2013-10-05T19:00:14.847 回答