首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP,从数据库中获取数据

问PHP,从数据库中获取数据
EN

Stack Overflow用户
提问于 2012-12-01 14:04:00
回答 2查看 13.8K关注 0票数 2

我想用PHP从数据库中检索数据,并在网站上显示它。

此代码不能正常工作。我想在我的数据库中显示雪佛兰的所有汽车。

代码语言:javascript
复制
<?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从数据库中获取数据?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-01 14:05:38

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

这就是它的工作原理

1)通过mysql_connect()连接数据库

代码语言:javascript
复制
mysql_connect("localhost", "username", "password") or die(mysql_error()); 

2)然后选择数据库,如mysql_select_db()

代码语言:javascript
复制
mysql_select_db("Database_Name") or die(mysql_error()); 

3)需要使用mysql_query()

喜欢

代码语言:javascript
复制
 $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()

代码语言:javascript
复制
  if($result){
         while($row= mysql_fetch_array( $result )) {
             //result
        }
      }

所以试一试

代码语言:javascript
复制
$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 ?)。

​

票数 5
EN

Stack Overflow用户

发布于 2012-12-01 14:05:53

代码语言:javascript
复制
<?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>"; 
 ?> 
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13656911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档