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-pdo-sqlsrv-5.9.0-8.0-nts-vs16-x64.zip
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
内附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
官方离线安装包,亲测可用
离线安装包,亲测可用
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
Laravel开发-laravel-generic-database 为Laravel4提供支持通用PDO驱动程序的数据库驱动程序
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
php7 安装依赖
PHP-PDO-MySQL类 一个类似于Python MySQLdbPHP MySQL PDO类,当使用“ WHERE IN”语句时,它支持迭代器和参数绑定。安装将src/下的文件复制到您的程序中要么composer require lincanbin/php-pdo-mysql-class初始化&...
PDO:Python数据库对象。 基于DB-API 2.0的Python数据库模块:具有按名称进行列访问和高性能。 API与ADO,JDBC类似。
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 --...
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 ...
离线安装包,亲测可用
官方离线安装包,亲测可用
离线安装包,亲测可用
pdo-数据库类-v2 PDO 数据库类 v2
离线安装包,亲测可用