- 浏览: 187338 次
- 性别:
- 来自: 北京
最新评论
-
heping45:
有待进步,不知所云
WEB流程图(jquery实现) -
zhoupuyue:
html有一个小错误:<script> $(fu ...
漂亮的Jquery的日期控件(精减) -
dcdc723:
happy175 写道大家是怎么用的啊,我的怎么没有反应?这是 ...
漂亮的Jquery的日期控件(精减) -
happy175:
大家是怎么用的啊,我的怎么没有反应?这是我的代码:
<! ...
漂亮的Jquery的日期控件(精减) -
nianshi:
加上Html文件多好啊
漂亮的Jquery的日期控件(精减)
Definitely, a good point to start demonstrating the excellent functionality provided by the PDO extension is in showing how it can be used to connect to diverse database systems. Therefore, I coded a simple script that establishes a new connection to MySQL, using the pertinent PDO constructor.
The signature of the script is as follows:
// example connecting to MySQL with the PDO extension
$dbh = new PDO('mysql:host=localhost;dbname=alejandro','user','password');
Certainly, after examining the above script, you'll have to agree with me that connecting to a specific database system, in this case MySQL, is a process that can be easily performed with the PDO library. The procedure is reduced to creating a new instance of the PDO class and passing to its constructor the proper connection parameters.
As you can see, the constructor accepts these incoming arguments in the form of a typical connection string (very similar to the notation used with ODBC databases), where the host is told where to connect, the selected database, and finally that the corresponding user/password combination must be supplied in the proper sequence.
Well, once a successful connection has been established to the specific database server, a new PDO object is returned to client code. This object allows the performance of a great variety of database-related tasks, which will be covered in detail over the new few lines, so don't worry about them for the moment.
Now, let me return to the previous script and show you how to close the connection that was opened to the specified MySQL server. This procedure is achieved as follows:
// example closing a connection to MySQL
$dbh = new PDO('mysql:host=localhost;dbname=alejandro','user','password');
// close MySQL connection by assigning a NULL value to PDO object
$dbh = NULL;
This isn't rocket science at all. As you can see, closing an established connection is performed by assigning a NULL value to the pertinent PDO object, as indicated above. Quite simple, right?
Now that you have learned how to use the PDO extension to open and close a connection to a sample MySQL database server, let me show you how to perform the same tasks with an Oracle system. Here is how these processes are done:
// example connecting to Oracle Call Interface
$dbh=new PDO('oci:','user','password');
// example closing a connection to Oracle Call Interface
$dbh=new PDO('oci:','user','password');
// close Oracle connection by assigning a NULL value to PDO
object
$dbh = NULL;
As you can see, the process of opening and closing a connection to an Oracle database server is nearly identical to the one utilized with MySQL. However, you may want to see more examples of connecting to different database systems. Below I included a few short scripts that demonstrate how to perform these connections. Please take a look.
//**************************************************
// example opening an ODBC connection
$dbh= new PDO('odbc:EXAMPLE','db2inst','ibmdb');
//**************************************************
// example closing an ODBC connection
$dbh= new PDO('odbc:EXAMPLE','db2inst','ibmdb');
// close ODBC connection by assigning a NULL value to PDO object
$dbh = NULL;
//**************************************************
// example opening a connection to SQLITE 2 (available in PHP
5.1)
$dbh= new PDO('sqlite2:/databases/mydb.sq2');
//**************************************************
// example closing a connection to SQLITE 2
$dbh= new PDO('sqlite2:/databases/mydb.sq2');
// close SQLITE2 connection by assigning a NULL value to PDO
object
$dbh = NULL;
As shown above, connecting to MySQL, Oracle or SQLite 2 is reduced to passing the proper connection parameters to the respective PDO constructor, since all of the tasks related to handling a specific database system are performed behind the scenes. Also, it's worthwhile to stress here that the connection strings used with all the previous examples may vary according to the database used. Therefore, I suggest you have a look at the PDO official documentation, located at http://ww.pdo.php.net, for further details.
At this point, you have learned how to connect to different database servers, but you may be wondering what happens if a particular connection fails. Well, the PDO library really shines in this aspect, since if an error occurs, a PDO exception (yes, you read correctly) is thrown, which allows it to easily handle any potential problem with elegance and efficiency.
This process is illustrated by the script below; please examine its signature.
// example using the PDO Exception (an exception is thrown when
an error occurs at connecting or when performing other tasks)
try{
$dbh=new PDO('mysql:host=localhost;dbname=alejandro',$user,$password);
}
catch(PDOException $e){
echo 'Error connecting to MySQL!: '.$e->getMessage();
exit();
}
As you can see, the above example clearly demonstrates how to use a PDO exception to handle a failed connection. However, the functionality of this proprietary exception mechanism doesn't stop here. Different exceptions will be triggered when performing queries, handling results sets, and so forth, which can be really useful for having all the eventual errors handled by a centralized module.
Okay, now that you have seen how to open and close a connection to different database systems, it's time to examine other methods included with the PDO extension. In this case, I'm going to show you how to run queries against a concrete database, so if you're interested on seeing how this will be achieved, click on the link below and keep reading.
发表评论
-
cassandra CONNECT
2012-02-25 12:22 1892<?php // Setting up no ... -
cgi 启动问题
2011-09-23 16:08 877/usr/local/bin/spawn-fcgi -a 12 ... -
Using PDO Objects in PHP 5 - Finding insertion IDs for database rows
2010-12-22 13:07 1061As I pointed out in the section ... -
Using PDO Objects in PHP 5 - Running queries against a specific
2010-12-22 12:41 1063Naturally, after learning ho ... -
discuz7.2 二次开发---------后台添加菜单
2010-09-15 15:13 1706menu.inc.php showmenu('te ... -
discuz 文件说明
2010-09-10 15:39 7421文件颜色说明:红色:程序核心文件,修改这类文件时千万要注意安全 ... -
discuz 7.2数据库表结构
2010-09-09 15:54 5937discuz 7.2数据库结构表 discuz, 结构, 数据 ... -
smarty变量操作符
2010-09-05 14:17 883capitalize [首字母大写] count_char ... -
eclipse自动补全
2010-09-05 11:11 1390eclipse自动补全配置 -
根据IP自动切换城市(腾讯网接口)
2010-05-27 10:02 2801<script src="http://fw. ... -
常用类
2010-05-04 10:41 798// 过滤html代码 function SpHtml2 ... -
用jcrop裁剪图片
2010-04-16 14:42 1169<? if ($_SERVER['REQUEST_ME ... -
图片加水印
2010-04-16 09:59 809$file="7.jpg"; $widt ... -
php创建验证字符图片
2010-04-16 09:24 854<?php //(1)创建背景图像。建立一幅150* ... -
php加密解密
2010-04-15 17:07 1229<? define('UC_KEY','finger ... -
icon生成类
2010-04-14 17:40 970<?php $output = "&qu ... -
jquery 操作复选框 取值
2010-02-26 17:50 1376<? //引入类 ?> <!DOCTY ... -
smarty 基本配置、应用
2010-02-23 21:40 1242//以下为:smart_inc.php include_o ... -
PHP 通用分页类PagerBox及使用
2010-02-04 21:36 1538<?php //使用实例 //$totalNum = ... -
PHP SOCKET()方式发送邮件(附php类)
2010-02-03 21:48 2419fsockopen(string hostname, ...
相关推荐
标题 "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完整包名] 进行安装
官方离线安装包,亲测可用。使用rpm -ivh [rpm完整包名] 进行安装
标题 "php-pdo-sqlsrv 各版本 和 sqlncli 64" 指涉的是 PHP 数据库扩展 PDO_SQLSRV 和 SQL Native Client (sqlncli) 的64位版本,这两个组件对于PHP开发者在Windows环境下与Microsoft SQL Server进行交互至关重要。...
oci8扩展提供了PHP与Oracle数据库交互的接口,而PDO_OCI扩展则遵循PHP Data Objects(PDO)标准,提供了一种统一的方式来访问Oracle数据库。下面将详细介绍如何在Linux系统上安装这两个扩展及其依赖。 首先,我们...
标题中的“php采用ms官方扩展pdo方式连接高版本mssql需要sqlncli”指的是在PHP环境中,使用微软官方提供的PDO(PHP Data Objects)扩展来连接SQL Server高版本数据库时,必须依赖sqlncli这个组件。PDO是PHP的一个...
首先,PDO(PHP Data Objects)是PHP中的一套数据库访问层,提供了一种统一的API来访问多种数据库系统,包括Oracle。而oci(Oracle Call Interface)是Oracle提供的C语言接口,用于在非Oracle环境下与Oracle数据库...
- 预处理语句:`$stmt = $pdo->prepare('INSERT INTO mytable (name, value) VALUES (?, ?)');` - 绑定参数并执行:`$stmt->execute(array('John', 'Doe'));` 4. **注意事项** - PDO_DBLIB不支持所有的SQL ...
1. **pdo_sqlsrv**:这是一个PDO(PHP Data Objects)驱动,遵循PDO接口标准,提供了一种统一的方式来访问不同的数据库系统。使用PDO的好处包括预处理语句、事务支持和更好的错误处理机制。 2. **sqlsrv**:这是非...
**PDO (PHP Data Objects)** 是PHP中的一个数据库访问抽象层,它提供了统一的方式去访问多种数据库系统。PDO的主要目标是提供一种轻量级、清晰且方便的API,使得开发者能够以一种统一的方式来访问不同的关系数据库...
本知识点主要围绕标题"php5.6+pdo+sqlserver 扩展dll"展开,讲解如何在PHP 5.6环境下通过PDO(PHP Data Objects)扩展来实现与SQL Server的连接,并使用相关的DLL扩展文件。 PDO是一种PHP的数据库抽象层,它提供了...
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { echo "Connection failed: " . $e->getMessage(); } } public function query($sql, $params = array()...
PDO_MYSQL是PHP的一个数据库访问接口,它允许PHP应用程序通过PHP Data Objects (PDO) API与MySQL数据库进行交互。PDO提供了统一的API,使得开发者可以轻松地在不同的数据库系统之间切换,而无需大幅度修改代码。 ...