gpt4 book ai didi

PHP,从数据库中获取数据

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

我想用 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 从数据库中获取数据?

最佳答案

您没有查询数据库,所以它不会给您结果

它是这样工作的

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());

查看是否出错

4) 比 mysql_fetch_array()

  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_* 函数已弃用,因此请使用 PDOMySQLi 代替。我建议 PDO 更容易阅读,您可以在这里学习 PDO Tutorial for MySQL Developers还要检查 Pdo for beginners ( why ? and how ?)

关于PHP,从数据库中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13656911/

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