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与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
官方离线安装包,亲测可用
离线安装包,亲测可用
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
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 --...
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
标题 "php-pdo-sqlsrv 各版本 和 sqlncli 64" 指涉的是 PHP 数据库扩展 PDO_SQLSRV 和 SQL Native Client (sqlncli) 的64位版本,这两个组件对于PHP开发者在Windows环境下与Microsoft SQL Server进行交互至关重要。...
标题中的“php采用ms官方扩展pdo方式连接高版本mssql需要sqlncli”指的是在PHP环境中,使用微软官方提供的PDO(PHP Data Objects)扩展来连接SQL Server高版本数据库时,必须依赖sqlncli这个组件。PDO是PHP的一个...
- 预处理语句:`$stmt = $pdo->prepare('INSERT INTO mytable (name, value) VALUES (?, ?)');` - 绑定参数并执行:`$stmt->execute(array('John', 'Doe'));` 4. **注意事项** - PDO_DBLIB不支持所有的SQL ...
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { echo "Connection failed: " . $e->getMessage(); } } public function query($sql, $params = array()...
oci8扩展提供了PHP与Oracle数据库交互的接口,而PDO_OCI扩展则遵循PHP Data Objects(PDO)标准,提供了一种统一的方式来访问Oracle数据库。下面将详细介绍如何在Linux系统上安装这两个扩展及其依赖。 首先,我们...
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = :email AND status = :status'); $stmt->execute(['email' => $email, 'status' => $status]); ``` **错误处理和事务支持** PDO提供了两种错误模式:...
PDO_MYSQL是PHP的一个数据库访问接口,它允许PHP应用程序通过PHP Data Objects (PDO) API与MySQL数据库进行交互。PDO提供了统一的API,使得开发者可以轻松地在不同的数据库系统之间切换,而无需大幅度修改代码。 ...
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 ...
离线安装包,亲测可用