gpt4 book ai didi

php - 检索我的基本公告数据库的最新条目

转载 作者:行者123 更新时间:2023-11-29 03:04:50 25 4
gpt4 key购买 nike

有些人可能知道我的脚本 Basic Announce,我正在尝试获取已发送的最新新闻条目,因此只调用最后知道的条目。

在此代码块中是我最初创建的新闻调用程序,但它决定调用所有条目,但那是供管理员查看的,但我需要在此文件中调用最后一个条目:news.php

<?php

// Connects to your Database

mysql_connect("$server","$usr","$pswd") or die(mysql_error());

mysql_select_db("$db") or die(mysql_error());

$data = mysql_query("SELECT * FROM announcements")

or die(mysql_error());



while($info = mysql_fetch_array( $data ))

{

Print "<tr>";

Print "<th>Announcement:</th><td>".$info['Announcement'] . "</td> ";

Print "<br>";

Print "<th>Submitted By:</th> <td>".$info['Submitted'] . "</td> ";

}

;

?>

我将如何选择最后知道的条目我还包括我的数据库表 sql 代码。

这是基本的 Announce.sql 代码

CREATE TABLE IF NOT EXISTS `announcements` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Announcement` text NOT NULL,
`Submitted` text NOT NULL,
`Date_time` date NOT NULL,
PRIMARY KEY (`id`)
);

是否可以使用我的主键“id”提取最后一条记录?

对于这个麻烦事的任何帮助将不胜感激,因为我仍在通过自学和灵感学习 PHP。

非常感谢

最佳答案

id降序排列:

$data = mysql_query("SELECT * FROM announcements ORDER BY id DESC LIMIT 1");

我还添加了 LIMIT 1 因为您只想检索集合中的第一行。如果您只需要一行,则不需要 while 循环,只需调用一次 fetch 即可:

$data = mysql_query("SELECT * FROM announcements ORDER BY id DESC LIMIT 1");

if($data && mysql_num_rows($data) == 1) // check for success and a row found
{
$info = mysql_fetch_array( $data );

Print "<tr>";
Print "<th>Announcement:</th><td>".$info['Announcement'] . "</td> ";
Print "<br>";
Print "<th>Submitted By:</th> <td>".$info['Submitted'] . "</td> ";
}
else
{
// no rows or an error occurred
}

旁注:

  • mysql_* 库已弃用。对于新代码,您应该考虑升级到 PDO 或 MySQLi。

  • .. or die(mysql_error()) 应该避免。更好的选择是 trigger_error(mysql_error())

关于php - 检索我的基本公告数据库的最新条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17402856/

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