我想用PHP从数据库中检索数据,并在网站上显示它。
此代码不能正常工作。我想在我的数据库中显示雪佛兰的所有汽车。
<?php
$db = mysqli_connect("localhost","myusername",
"mypassword","database");
if (mysqli_connect_errno()) {
echo("Could not connect" .
mysqli_connect_error($db) . "</p>");
exit(" ");
}
$result = mysqli_query($query);
if(!$result){
echo "<p> Could not retrieve at this time, please come back soon</p>" .
mysqli_error($dv);
}
$data = mysql_query("SELECT * FROM cars where carType = 'Chevy' AND active = 1")
or die(mysql_error());
echo"<table border cellpadding=3>";
while($row= mysql_fetch_array( $data ))
{
echo"<tr>";
echo"<th>Name:</th> <td>".$row['name'] . "</td> ";
echo"<th>ImagePath:</th> <td>".$row['imagePath'] . " </td></tr>";
echo"<th>Description:</th> <td>".$row['description'] . "</td> ";
echo"<th>Price:</th> <td>".$row['Price'] . " </td></tr>";
}
echo"</table>";
?>如何使用PHP从数据库中获取数据?
发布于 2012-12-01 14:05:38
你没有查询数据库,所以它不会给你结果
这就是它的工作原理
1)通过mysql_connect()连接数据库
mysql_connect("localhost", "username", "password") or die(mysql_error()); 2)然后选择数据库,如mysql_select_db()
mysql_select_db("Database_Name") or die(mysql_error()); 3)需要使用mysql_query()
喜欢
$query = "SELECT * FROM cars where carType = 'chevy' AND active = 1";
$result =mysql_query($query); //you can also use here or die(mysql_error()); 查看是否存在错误
if($result){
while($row= mysql_fetch_array( $result )) {
//result
}
}所以试一试
$data = mysql_query("SELECT * FROM cars where carType = 'chevy' AND active = 1") or die(mysql_error());
echo"<table border cellpadding=3>";
while($row= mysql_fetch_array( $data ))
{
echo"<tr>";
echo"<th>Name:</th> <td>".$row['name'] . "</td> ";
echo"<th>ImagePath:</th> <td>".$row['imagePath'] . " </td></tr>";
echo"<th>Description:</th> <td>".$row['description'] . "</td> ";
echo"<th>Price:</th> <td>".$row['Price'] . " </td></tr>";
}
echo"</table>";
?>
备注:
不推荐使用Mysql_*函数,因此请改用PDO或MySQLi。我建议使用PDO,它更容易阅读,你可以在这里学习,也可以查看Pdo for beginners ( why ? and how ?)。
发布于 2012-12-01 14:05:53
<?php
// Connects to your Database
mysql_connect("hostname", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
$data = mysql_query("SELECT * FROM cars where cars.carType = 'chevy' AND cars.active = 1")
or die(mysql_error());
Print "<table border cellpadding=3>";
while($row= mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$row['name'] . "</td> ";
Print "<th>ImagePath:</th> <td>".$row['imagePath'] . " </td></tr>";
Print "<th>Description:</th> <td>".$row['description'] . "</td> ";
Print "<th>Price:</th> <td>".$row['Price'] . " </td></tr>";
}
Print "</table>";
?> https://stackoverflow.com/questions/13656911
复制相似问题