【问题标题】:Take the last primary key id enter in the database to var取数据库中输入的最后一个主键 id 到 var
【发布时间】:2014-05-28 13:18:06
【问题描述】:

我需要选择我在数据库中输入的最后一个id 并将其放入一个var中。我在 PHP 中创建我的 var:

$timId = "" ;

但我尝试过这样的事情:

$requete = $bdd->prepare('SELECT LAST(`timId`)  FROM `timesheets`   ');
$requete->execute(array('timId'=> $_POST['timId'])) or die(print_r($requete->errorInfo()));
$resultat = $requete->fetch();
$timId = $resultat['timId'];
echo($timId);   

我知道我的 SQL 语法是错误的,但我该怎么做呢?

【问题讨论】:

  • 如果 id 是自动递增和主键,你想使用 'order by id desc'

标签: php sql key var


【解决方案1】:

通常如果使用mysql,你应该使用“select last_insert_id()”

当然,这假设您使用的是 auto_increment 列。

另外,根据 $bdd 的类,它可能有一个方法来获取最后插入的 id。

【讨论】:

  • 我正在使用 phpmyadmin 在我的数据库中尝试我的请求。
【解决方案2】:
Use Order BY id Desc , Using limit clause you can achieve this 

【讨论】:

  • 在这里感谢正确的语法:SELECT timId FROM timesheets ORDER BY timId Desc LIMIT 1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-01
  • 2023-03-14
  • 1970-01-01
  • 2011-06-10
相关资源
最近更新 更多