`
dcdc723
  • 浏览: 187348 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Using PDO Objects in PHP 5 - Running queries against a specific

    博客分类:
  • PHP
阅读更多

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-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-pdo-sqlsrv-5.9.0-8.0-nts-vs16-x64.zip" 暗示了这是一个针对PHP的扩展,用于连接到SQL Server数据库。这个扩展是PDO(PHP Data Objects)的一个实现,它提供了一个统一的API来访问不同的数据库管理系统,...

    php72w-pdo-7.2.34-1.w7.x86-64.rpm

    php7 安装依赖

    msodbcsql(32+64),php_pdo_sqlsrv(5.3-5.6),内附thinkphp5多数据库连接教程,thinkphp5连接sql seve

    内附msodbcsql(32+64)安装包+php_pdo_sqlsrv(5.3-5.6)配置文件,外加thinkphp5多数据库连接教程,和thinkphp5连sql server Demo,清晰易懂

    php8-mssql.zip

    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

    php-pdo-7.4.19-1.module_el8.5.0+815+1c4fd2e5.aarch64.rpm

    官方离线安装包,亲测可用

    php-pdo-7.3.20-1.module_el8.2.0+498+4deef2f1.aarch64.rpm

    离线安装包,亲测可用

    rh-php73-php-pdo-7.3.29-1.el7.x86_64.rpm

    官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装

    rh-php73-php-pdo-7.3.20-1.el7.x86_64.rpm

    官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装

    php-pdo-5.4.16-48.el7.x86_64.rpm

    官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装

    php-pdo-sqlsrv 各版本 和 sqlncli 64

    标题 "php-pdo-sqlsrv 各版本 和 sqlncli 64" 指涉的是 PHP 数据库扩展 PDO_SQLSRV 和 SQL Native Client (sqlncli) 的64位版本,这两个组件对于PHP开发者在Windows环境下与Microsoft SQL Server进行交互至关重要。...

    php-7.4.33-centos7下编译,含libonig.so.5及libicu*.so.50

    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采用ms官方扩展pdo方式连接高版本mssql需要sqlncli

    标题中的“php采用ms官方扩展pdo方式连接高版本mssql需要sqlncli”指的是在PHP环境中,使用微软官方提供的PDO(PHP Data Objects)扩展来连接SQL Server高版本数据库时,必须依赖sqlncli这个组件。PDO是PHP的一个...

    PHP7.3的sql server PDO_DBLIB库

    - 预处理语句:`$stmt = $pdo-&gt;prepare('INSERT INTO mytable (name, value) VALUES (?, ?)');` - 绑定参数并执行:`$stmt-&gt;execute(array('John', 'Doe'));` 4. **注意事项** - PDO_DBLIB不支持所有的SQL ...

    Windows-7.4-PHP适配SQLserver驱动

    1. **pdo_sqlsrv**:这是一个PDO(PHP Data Objects)驱动,遵循PDO接口标准,提供了一种统一的方式来访问不同的数据库系统。使用PDO的好处包括预处理语句、事务支持和更好的错误处理机制。 2. **sqlsrv**:这是非...

    PHP-PDO数据库操作类

    $this-&gt;pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { echo "Connection failed: " . $e-&gt;getMessage(); } } public function query($sql, $params = array()...

    LINUX下安装PHP的oci8和PDO_OCI扩展包

    oci8扩展提供了PHP与Oracle数据库交互的接口,而PDO_OCI扩展则遵循PHP Data Objects(PDO)标准,提供了一种统一的方式来访问Oracle数据库。下面将详细介绍如何在Linux系统上安装这两个扩展及其依赖。 首先,我们...

    -Php-Pdo-ile-Blog-Sitesi

    $stmt = $pdo-&gt;prepare('SELECT * FROM users WHERE email = :email AND status = :status'); $stmt-&gt;execute(['email' =&gt; $email, 'status' =&gt; $status]); ``` **错误处理和事务支持** PDO提供了两种错误模式:...

    PDO_MYSQL-1.0.2.tgz

    PDO_MYSQL是PHP的一个数据库访问接口,它允许PHP应用程序通过PHP Data Objects (PDO) API与MySQL数据库进行交互。PDO提供了统一的API,使得开发者可以轻松地在不同的数据库系统之间切换,而无需大幅度修改代码。 ...

    php的serverSQL 2008的带PDO扩展支持

    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 ...

Global site tag (gtag.js) - Google Analytics