设为首页 收藏本站
查看: 630|回复: 0

[经验分享] Using PDO Objects in PHP 5

[复制链接]

尚未签到

发表于 2017-4-10 11:27:26 | 显示全部楼层 |阅读模式
  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.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-362870-1-1.html 上篇帖子: php静态化页面——htaccess写法详解(htaccess怎么写?) 下篇帖子: cas sso 集成 java + php (discuz,bbs) 配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表