As I pointed out in the section you just read, the PDO extension also offers a handy method, called "lastInsertId()," which is useful in those situations where it's necessary to find out the ID of the last-inserted database row.
The implementation of the method is very straightforward, as you can see in the example below:
/ example using the 'lastInsertId()' method (returns the ID of
last inserted row)
try{
$dbh=new PDO('mysql:host=localhost;dbname=alejandro','user','password');
$dbh->query("INSERT INTO users SET
name='Alejandro',address='Nowhere',email=
'alejandro@domain.com'");
$insertId=$dbh->lastInsertId();
echo 'ID of last-inserted row after executing SQL
statement is as following: '.$insertId;
}
catch(PDOException $e) {
echo 'Error : '.$e->getMessage();
exit();
}
Finding the ID of the last-inserted database row is an easy-to-perform task, thanks to the excellent functionality provided by the "lastInsertId()" method. Similar to the approach followed with previous examples, in this case I used the MySQL server to demonstrate how this method works, but as you saw earlier, this condition can be easily modified to work with a different database system.
As usual with many of my articles on PHP development, feel free to introduce your own modifications to all the hands-on examples shown here, so you can acquire a more robust grounding in how to use the most important features offered by the PDO extension. Fun is already guaranteed!
Final thoughts
In this first part of the series, I walked you through the key points of how to use the PDO extension that comes bundled with 5.1 and up. As was demonstrated by the hands-on examples included in this article, this library definitely makes working with multiple database systems a painless process.
Nonetheless, I have to admit that I'm only scratching the surface when it comes to exploring the numerous features offered by PHP Data Objects. So, considering the long way ahead of us, in the next tutorial I'm going to show you how to use this powerful PHP extension to manipulate results sets regardless of the database system you use.
Now that you've been warned, you won't want to miss it!
分享到:
相关推荐
标题 "PHP-MySQL-PDO-Database-Class-master" 暗示了这是一个关于使用PHP与MySQL数据库交互的项目,特别强调了使用PDO(PHP Data Objects)扩展。PDO是PHP中的一个数据库访问层,提供了统一的API来连接多种数据库,...
标题 "php-pdo-sqlsrv-5.9.0-8.0-nts-vs16-x64.zip" 暗示了这是一个针对PHP的扩展,用于连接到SQL Server数据库。这个扩展是PDO(PHP Data Objects)的一个实现,它提供了一个统一的API来访问不同的数据库管理系统,...
php7 安装依赖
内附msodbcsql(32+64)安装包+php_pdo_sqlsrv(5.3-5.6)配置文件,外加thinkphp5多数据库连接教程,和thinkphp5连sql server Demo,清晰易懂
php8连接mssql所需要的几个文件 php_pdo_sqlsrv-5.9.0-8.0-ts-vs16-x64 php_sqlsrv-5.9.0-8.0-ts-vs16-x64 VC_redist.x64 SQLSRV58 msodbcsql
在本文中,我们将深入探讨“Laravel开发-laravel-generic-database”这个项目,它为Laravel4框架提供了对通用PDO(PHP Data Objects)驱动程序的支持。Laravel是一款流行的开源PHP框架,以其优雅的语法和强大的功能...
官方离线安装包,亲测可用
离线安装包,亲测可用
在PDO-1.3.2这个版本中,可能包含了一些关键的改进和修复,例如对不同数据库驱动的支持增强,性能优化,bug修复,以及可能的新功能。为了充分利用PDO的功能,开发者需要根据所使用的数据库系统安装相应的PDO驱动,如...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
标题中的“php采用ms官方扩展pdo方式连接高版本mssql需要sqlncli”指的是在PHP环境中,使用微软官方提供的PDO(PHP Data Objects)扩展来连接SQL Server高版本数据库时,必须依赖sqlncli这个组件。PDO是PHP的一个...
1. **pdo_sqlsrv**:这是一个PDO(PHP Data Objects)驱动,遵循PDO接口标准,提供了一种统一的方式来访问不同的数据库系统。使用PDO的好处包括预处理语句、事务支持和更好的错误处理机制。 2. **sqlsrv**:这是非...
PHP-PDO-MySQL类 一个类似于Python MySQLdbPHP MySQL PDO类,当使用“ WHERE IN”语句时,它支持迭代器和参数绑定。安装将src/下的文件复制到您的程序中要么composer require lincanbin/php-pdo-mysql-class初始化&...
gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --...
标题 "php-pdo-sqlsrv 各版本 和 sqlncli 64" 指涉的是 PHP 数据库扩展 PDO_SQLSRV 和 SQL Native Client (sqlncli) 的64位版本,这两个组件对于PHP开发者在Windows环境下与Microsoft SQL Server进行交互至关重要。...
we are reaching an important milestone by releasing a Community Technology Preview (CTP) of the new SQL Server Driver for PHP 2.0, which includes support for PHP Data Objects (PDO). Alongside our ...
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = :email AND status = :status'); $stmt->execute(['email' => $email, 'status' => $status]); ``` **错误处理和事务支持** PDO提供了两种错误模式:...
- 预处理语句:`$stmt = $pdo->prepare('INSERT INTO mytable (name, value) VALUES (?, ?)');` - 绑定参数并执行:`$stmt->execute(array('John', 'Doe'));` 4. **注意事项** - PDO_DBLIB不支持所有的SQL ...