gpt4 book ai didi

php - 显示 mysql 数据库的结果

转载 作者:行者123 更新时间:2023-11-29 17:49:08 24 4
gpt4 key购买 nike

正在开发一个显示汽车列表的项目

$username="username";
$password="password";
$database="listofcars";
$mysqli = new mysqli("localhost", $username, $password, $database);
@mysql_select_db($database) or die( "Unable to select database");
$query2="SELECT * FROM cars";
$result=$mysqli->query($query2);
$num=$mysqli->mysqli_num_rows($result);
$mysqli->close();
echo "
<div class="item">
<div class="container">
<div class="imgcontainer"><img alt="Cars for sale" src="$carimage" width="380" height="380" /></div>
<div class="details">
<a href="$internallink" target="_blank">
<h3 class="title"> $carname <br />
<span> $cartype </span></h3>
<p>
$cardesc
</p>
<div class="button"><span data-hover="Order Car">Order Car</span></div>
</a>
</div>
</div>
</div>

我需要它来循环结果

数据库名称:listofcars表名:汽车

字段名称

卡西德 车名 卡图像 车型 内部链接 卡片式

最佳答案

尝试一下这个方法是否有效。

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "listofcars";

// Create connection
$mysqli = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$mysqli) {
die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT * FROM cars";
$result = mysqli_query($mysqli, $sql);

?>
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<div class="item">
<div class="container">
<div class="imgcontainer">
<img alt="Cars for sale" src="<?php echo $row['carimage']; ?>" width="380" height="380">
</div>
<div class="details">
<a href="<?php echo $row['internallink']; ?>" target="_blank">
<h3 class="title"><?php echo $row['carname']; ?><br><span><?php echo $row['cartype']; ?></span></h3>
<p><?php echo $row['cardesc']; ?></p>
<div class="button">
<span data-hover="Order Car">Order Car</span>
</div>
</a>
</div>
</div>
</div>
<?php } ?>

mysqli_close($mysqli);

关于php - 显示 mysql 数据库的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49541921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com