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

时间:2022-08-04 22:13:53

How to display 10 latest jobs from database like this. Please click here to see image

如何从这样的数据库中显示10个最新作业。请点击这里查看图片

I have written following code so far not sure what to do next.

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

$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');

}

The $row returns all the columns from database. I want only four job title, company name, location and date

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

4 个解决方案

#1


1  

Use column names in select query.

在选择查询中使用列名称。

$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>";

#2


0  

Hmm change the query

嗯改变查询

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

#3


0  

You can order by for rownum

您可以通过rownum订购

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

#4


0  

Here is an example how to build a htrml table with the 4 columns:

以下是如何使用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>";

#1


1  

Use column names in select query.

在选择查询中使用列名称。

$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>";

#2


0  

Hmm change the query

嗯改变查询

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

#3


0  

You can order by for rownum

您可以通过rownum订购

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

#4


0  

Here is an example how to build a htrml table with the 4 columns:

以下是如何使用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>";