这篇文章主要PHP中PDO的三种数据库连接方式,有一定的参考价值,感兴趣的朋友可以参考一下。
在使用PDO连接数据库之前,先要对PDO安装和配置
base.php如下:
<strong><span style="font-size:18px;"><?php header('Content-Type:text/html;charset=utf-8'); //数组调试函数 function show_bug($msg){ echo '<pre class="brush:php;toolbar:false">'; print_r($msg); echo ''; } ?>
1.pdo通过参数形式链接数据库
<strong><span style="font-size:18px;">include_once "base.php"; try{ //$dsn是数据源 $dsn='mysql:host=localhost;dbname=imooc'; $username='root'; $passwd=''; $pdo=new PDO($dsn,$username,$passwd); //如果连接成功的话,得到的是pdo的对象 show_bug($pdo); }catch(PDOException $e){ echo $e->getMessage(); }</span></strong>
2.pdo通过uri形式连接数据库
<strong><span style="font-size:18px;"><?php include_once "base.php"; //pdo通过uri形式连接数据库 try{ $dsn='uri:file://D:\wamp\www\muke\pdo\dsn.txt'; $username='root'; $passwd=''; $pdo=new PDO($dsn,$username,$passwd); show_bug($pdo); }catch(PDOException $e){ echo $e->getMessage(); } ?></span></strong>
3.pdo通过配置文件形式连接数据库
<strong><span style="font-size:18px;"><?php include_once "base.php"; //pdo通过配置文件形式连接数据库 //在php.ini中配置 try{ $dsn='imooc'; $username='root'; $passwd=''; $pdo=new PDO($dsn,$username,$passwd); show_bug($pdo); }catch(PDOException $e){ echo $e->getMessage(); } ?></span></strong>
以上三种,建议使用第一种,通过参数形式链接数据库的
【相关教程推荐】
1. php编程从入门到精通全套视频教程
2. php从入门到精通
3. bootstrap教程

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6
Visual web development tools

Atom editor mac version download
The most popular open source editor
