Naturally, after learning how to open and close a connection to a specific database system, I'm sure that you'll want to see how to execute a query against a particular database. To perform this crucial task, the PDO extension comes equipped with two primary methods, not surprisingly called "query()" and "exec()" respectively.
However, the two methods behave slightly differently. The second one, that is "exec()," not only performs a specified query, but returns the number of affected rows after performing an "INSERT," "UPDATE" or "DELETE" command.
Now that you see the difference between both methods, please study the following examples. They demonstrate separately how to use each of the methods in question, first for connecting to MySQL, and then for fetching some rows from a sample "USERS" database.
The respective code listings are as follows:
// example using the 'query()' method
try{
$dbh=new PDO('mysql:host=localhost;dbname=alejandro','user','password');
foreach($dbh->query('SELECT * FROM users') as $row){
echo 'Name :'.$row['name'].' Postal Address :'.$row['address'].' Email :'.$row['email'].'<br />';
}
}
catch(PDOException $e){
echo 'Error : '.$e->getMessage();
exit();
}
// example using the 'exec()' method (returns the number of rows
affected by the query)
try{
$dbh=new PDO('mysql:host=localhost;dbname=alejandro','user','password');
$delrows=$dbh->exec('DELETE FROM users WHERE id<20');
echo 'Number of deleted rows is the
following :'.$delrows;
}
catch(PDOException $e){
echo 'Error : '.$e->getMessage();
exit();
}
As shown previously, the first example illustrates the implementation of the handy "query()" method to fetch some rows from a simple database table, while the second case shows how to use "exec()" to run a "DELETE" statement, and finally display the number of deleted rows.
It's worth clarifying here that all of the above examples use a MySQL server, but as you learned in the previous section, the same methods can be utilized with Oracle, MS SQL, SQLite or whatever database system you need to include in your own PHP applications.
So far, so good. At this stage you have hopefully learned how to run queries with the PDO library utilizing the two primary methods bundled with the extension. So what's the next step? Well, in the last section of this tutorial I'm going to show you how to use a brand new method, called "lastInsertId()," which obviously returns the ID of the last-inserted database row.
To learn how this method can be implemented, go ahead and read the new few lines. I'll be there, waiting for you.
分享到:
相关推荐
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完整包名] 进行安装
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
php7 安装依赖
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 --...
Alongside our efforts, the Commerce Guys, a company providing ecommerce solutions with Drupal, is also presenting a beta version of Drupal 7 running on SQL Server using this new PDO Application ...
离线安装包,亲测可用
离线安装包,亲测可用
官方离线安装包,亲测可用
离线安装包,亲测可用
-Php-Pdo-Blog-Site
官方离线安装包,亲测可用
PDO_MYSQL-1.0.2.tgz PDO_MYSQL-1.0.2.tgz
PDO:Python数据库对象。 基于DB-API 2.0的Python数据库模块:具有按名称进行列访问和高性能。 API与ADO,JDBC类似。