简体   繁体   English

如何使用PDO查找数据库表的最后一条记录?

[英]How do I find the last record my database table using PDO?

How can I execute this using PDO? 如何使用PDO执行此操作? I use MySQL for my database. 我将MySQL用于数据库。 I am trying to call the last infog_id . 我正在尝试调用最后一个infog_id

$q = $conn->query("SELECT * FROM Infographic ORDER BY infog_id DESC LIMIT 1");
$q->fetchAll(); 

Similar to what Sean said, if you only need one column, don't get all of them. 与肖恩(Sean)所说的类似,如果只需要一列,就不要全部使用。

$q = $conn->query("SELECT infog_id FROM Infographic ORDER BY infog_id DESC LIMIT 1");
$infog_id = $q->fetchColumn();

fetchColumn() by default retrieves the first column from the next available row, for this query, this will be infog_id. 默认情况下,fetchColumn()从下一个可用行中检索第一列,对于此查询,该列将为infog_id。

If you actually want the whole row, use fetch(). 如果您实际上想要整个行,请使用fetch()。

$q = $conn->query("SELECT * FROM Infographic ORDER BY infog_id DESC LIMIT 1");
$row = $q->fetch();

fetch() returns the next available row, in this case, there is only one (LIMIT 1). fetch()返回下一个可用行,在这种情况下,只有一行(LIMIT 1)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM