如何从数据库中获取10条最新记录

时间:2016-05-31 08:04:36

标签: php mysql

如何从数据库中显示10个最新作业。 Please click here to see image

到目前为止,我编写了以下代码,不知道下一步该做什么。

$query = "SELECT * FROM jobs ORDER BY posting_date DESC LIMIT 10";

$query = mysqli_query($conn, $query);

while ($row = mysqli_fetch_assoc($query) ) {


  $job_title = row["job_title"];
  $company_name = $row["company_name"];
  $department = $row["department"];
  $location = $row["location"];   
  $job_type = $row["job_type"];   
  $job_description = $row["job_description"];
  $posting_date = date('d-m-y');

}

$ row返回数据库中的所有列。我只想要四个职位,公司名称,地点和日期

4 个答案:

答案 0 :(得分:1)

在选择查询中使用列名。

$query = "SELECT job_title,company_name,location,posting_date FROM jobs ORDER BY posting_date DESC LIMIT 10";
$query = mysqli_query($conn, $query);

echo "<table>";
echo "<tr> <th>Job Title</th> <th>Company Name</th> <th>Location</th> <th>Date Posted</th> </tr>";
echo "<tbody>";  
while($row = mysqli_fetch_assoc($query) ) {
  $job_title = $row["job_title"];
  $company_name = $row["company_name"];
  $department = $row["department"];
  $location = $row["location"];   
  $posting_date = date('d-m-y', strtotime($row['posting_date']));

  echo "<tr>";
  echo "<td>".$job_title."</td>";
  echo "<td>".$company_name."</td>";
  echo "<td>".$location."</td>";
  echo "<td>".$posting_date."</td>";
  echo "<tr>";
}
echo "</tbody>";
echo "</table>";

答案 1 :(得分:0)

嗯更改查询

$query = "SELECT job_title, company_name, location,  posting_date FROM jobs ORDER BY posting_date DESC LIMIT 10";

答案 2 :(得分:0)

您可以订购rownum

$query = "SELECT job_title, company_name, location,  posting_date FROM jobs ORDER BY rownum DESC LIMIT 10";

答案 3 :(得分:0)

以下是如何使用4列构建htrml表的示例:

$query = "SELECT * FROM jobs ORDER BY posting_date DESC LIMIT 10";
$query = mysqli_query($conn, $query);

echo "<table>";
while ($row = mysqli_fetch_assoc($query) ) {
  echo "<tr>";
  echo "<td>$row['job_title']</td>";
  echo "<td>$row['company_name']</td>";
  echo "<td>$row['location']</td>";
  echo "<td>$row['posting_date']</td>";
  echo "<tr>";
}
echo "<table>";