PHP,从数据库中获取数据
我想用PHP从数据库中检索数据,并将其显示在网站上 此代码无法正常工作。我想在我的数据库中显示所有雪佛兰汽车PHP,从数据库中获取数据,php,mysql,Php,Mysql,我想用PHP从数据库中检索数据,并将其显示在网站上 此代码无法正常工作。我想在我的数据库中显示所有雪佛兰汽车 <?php $db = mysqli_connect("localhost","myusername", "mypassword","database"); if (mysqli_connect_errno()) { echo("Could not connect" . mysqli_connect_error($db) . "</p>");
<?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>";
?>
您没有查询数据库,所以它不会给您结果
这就是它的工作原理
1) 通过以下方式连接到数据库:
2) 然后选择数据库,如
3) 你需要使用
像
看看是否有错误
4) 及
所以试试看
$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>";
?>
$data=mysql\u query(“从carType='chevy'和active=1的汽车中选择*”)或die(mysql\u error());
回声“;
while($row=mysql\u fetch\u数组($data))
{
回声“;
echo“Name:”.$row['Name']。”;
回显“ImagePath:.$row['ImagePath']。”;
echo“Description:”.$row['Description']。”;
echo“价格:”.$row[“价格]”;
}
回声“;
?>
注意:Mysql.*
函数已被弃用,因此请改用PDO
或MySQLi
。我建议PDO更容易阅读,您可以在这里学习,也可以检查您没有查询数据库,所以它不会给您结果。如果在mysql\u fetch\u array
之前删除@
,您会得到什么错误/警告?我编辑了我的问题,并根据您的建议修改了我的代码。不过我还是保留了我的QLI。我是php和mysql新手,所以我不确定我是否做对了。你应该使用mysql_*或mysqli…你所做的是连接mysqli并通过mysqli_*进行查询,所以它不会工作。请尝试回答中的代码并查看回答末尾的链接
mysql_select_db("Database_Name") or die(mysql_error());
$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>";
?>
<?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>";
?>