PDO方式连接数据库、PHP连接数据库
<span style="color: black;">点击进入“PHP开源社区” </span><span style="color: black;">免费获取进阶面试、文档、视频资源</span><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">1.什么是PDO?</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">PDO是PHP Date Object(PHP数据对象)的简<span style="color: black;">叫作</span>,它是与PHP 5.1版本<span style="color: black;">一块</span>发行的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">日前</span>支持的数据库<span style="color: black;">包含</span>Firebird、FreeTDS、Interbase、MySQL、MS SQL Server、ODBC、Oracle、Postgre SQL、SQLite和Sybase。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">有了PDO,您不必再<span style="color: black;">运用</span>mysql_*函数、oci_*函数<span style="color: black;">或</span>mssql_*函数,<span style="color: black;">亦</span>不必再为它们封装数据库操作类,只<span style="color: black;">必须</span><span style="color: black;">运用</span>PDO接口中的<span style="color: black;">办法</span>就<span style="color: black;">能够</span>对数据库进行操作。在<span style="color: black;">选取</span><span style="color: black;">区别</span>的数据库时,只需修改PDO的DSN(数据源名<span style="color: black;">叫作</span>)。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在PHP 6中将默认<span style="color: black;">运用</span>PDO连接数据库,所有非PDO扩展将会在PHP 6中被移除。该扩展<span style="color: black;">供给</span>PHP内置类PDO来对数据库进行<span style="color: black;">拜访</span>,<span style="color: black;">区别</span>数据库<span style="color: black;">运用</span>相同的<span style="color: black;">办法</span>名,以<span style="color: black;">处理</span>数据库连接不统一的问题。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;"><span style="color: black;">2.PDO的<span style="color: black;">功效</span>与特点</span></strong></h1>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">统一<span style="color: black;">各样</span>数据库的<span style="color: black;">拜访</span>接口</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">PDO是一个“数据库<span style="color: black;">拜访</span>抽象层”,<span style="color: black;">功效</span>是统一<span style="color: black;">各样</span>数据库的<span style="color: black;">拜访</span>接口,与mysql和mssql函数库相比,PDO让跨数据库的<span style="color: black;">运用</span>更<span style="color: black;">拥有</span>亲和力;与ADODB和MDB2相比,PDO更<span style="color: black;">有效</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/vkBG6ju8OBuysyEKXlBPlPJ6iajQicIeCib4WDsVMx9icKrXDpGcFYWIicTL1g2bN6mlMGoAwTrtlusW4yvgmcF4M8Q/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">3.PDO的启用</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">PDO是与PHP 5.1<span style="color: black;">一块</span>发行的,默认<span style="color: black;">包括</span>在PHP 5.1中。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">因为</span>PDO<span style="color: black;">必须</span>PHP 5.0核心面向对象特性的支持,<span style="color: black;">因此呢</span>其<span style="color: black;">没</span>法在PHP 5.0之前的版本中<span style="color: black;">运用</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">默认<span style="color: black;">状况</span>下,PDO在PHP 5.2中为开启状态,<span style="color: black;">然则</span>要启用对某个数据库驱动程序的支持,仍<span style="color: black;">必须</span>进行相应的配置操作。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在Windows环境下,PDO在php.ini文件中进行配置,<span style="color: black;">倘若</span>想支持某个特定数据库,只<span style="color: black;">必须</span>把php.ini 文件里边所对应的 ;号 去掉<span style="color: black;">就可</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>先找到php.ini 配置文件,<span style="color: black;">而后</span>进入</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">搜索 </span><span style="color: black;">pdo_</span><span style="color: black;">可以看到<span style="color: black;">咱们</span><span style="color: black;">这儿</span>只打开了 mysql和sqlite。<span style="color: black;">重视</span>:开启过后<span style="color: black;">必须</span>重启Apache</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/vkBG6ju8OBuysyEKXlBPlPJ6iajQicIeCibrGRiamUht2vPxicbYo2PH4OKuPwLAR8MTv8IGaCJW4h5ibUam8yqP99Jg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span> phpinfo() 函数来查看<span style="color: black;">是不是</span>开启。</span></strong></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/vkBG6ju8OBuysyEKXlBPlPJ6iajQicIeCibwT4s39NTgDHJSxyb0FsnMcKw97Ls8fNwHefeiaox2vkOfZkeNQUlxicw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">能够</span>看到<span style="color: black;">咱们</span>成功开启!</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/vkBG6ju8OBuysyEKXlBPlPJ6iajQicIeCibg9nSeupanRtalX9icScQFqCT8dt9l4M9aHY9htAM9pEak3Dia0OyaKmw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">4.<span style="color: black;">经过</span>PDO连接数据库</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"> PDO的构造函数</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在PDO中,要<span style="color: black;">创立</span>与数据库的连接<span style="color: black;">必须</span>实例化PDO的构造函数,PDO构造函数的语法如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">__construct(string $dsn[,string $username[,string $password[,array $driver_options]]])</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">dsn:数据源名,<span style="color: black;">包含</span>主机名、端口号和数据库名<span style="color: black;">叫作</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">username:连接数据库的用户名。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">password:连接数据库的<span style="color: black;">暗码</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">driver_options:连接数据库的其他选项。</p>格式:mysql,db_databvase17,127.0.0.1,root,123456
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">实例:</span></p><span style="color: black;">$dbms=<span style="color: black;">mysql</span>;<span style="color: black;">//数据库类型</span></span><span style="color: black;">$host=<span style="color: black;">127.0.0.1</span>;<span style="color: black;">//数据库主机名 <span style="color: black;">亦</span><span style="color: black;">能够</span>是127.0.0.1</span></span><span style="color: black;">$dbName=<span style="color: black;">db_database17</span>;<span style="color: black;">//<span style="color: black;">运用</span>的数据库名<span style="color: black;">叫作</span></span></span><span style="color: black;">$user=<span style="color: black;">root</span>;<span style="color: black;">//用户名</span></span><span style="color: black;">$pass=<span style="color: black;">123456</span>;<span style="color: black;">//对应的<span style="color: black;">暗码</span></span></span><span style="color: black;">$dsn=<span style="color: black;">"$dbms:dbname=$dbName;host=$host"</span>;</span><span style="color: black;"> $conn= <span style="color: black;">new</span> PDO($dsn,$user,$pass);<span style="color: black;">//初始化一个PDO对象,<span style="color: black;">便是</span>创建了连接数据库的对象 $conn</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">构造函数用于构造PDO对象,有了这个对象,<span style="color: black;">咱们</span>我就<span style="color: black;">能够</span><span style="color: black;">运用</span>里边的<span style="color: black;">办法</span>来<span style="color: black;">拜访</span>数据库。</span></p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><strong style="color: blue;"><span style="color: black;">1).exec()<span style="color: black;">办法</span> 返回执行后受影响的行数</span></strong></h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">exec()<span style="color: black;">办法</span>返回执行后受影响的行数,:</p><span style="color: black;"><span style="color: black;">一般</span>用于 INSERT</span>、<span style="color: black;">DELETE</span>和<span style="color: black;">UPDATE</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">语句中。</p>实例:<span style="color: black;"><span style="color: black;">try</span>{</span><span style="color: black;"> $conn= <span style="color: black;">new</span> PDO($dsn,$user,$pass);<span style="color: black;">//初始化一个PDO对象,<span style="color: black;">便是</span>创建了连接数据库的对象 $conn</span></span><span style="color: black;"> $query=<span style="color: black;">"insert into tb_pdo_mysql(pdo_type,database_name,dates)values("</span>.$_POST[<span style="color: black;">pdo</span>].<span style="color: black;">","</span>.$_POST[<span style="color: black;">databases</span>].<span style="color: black;">","</span>.$_POST[<span style="color: black;">dates</span>].<span style="color: black;">")"</span>;</span><span style="color: black;"> $result=$conn->exec($query);</span><span style="color: black;"> <span style="color: black;">echo</span> <span style="color: black;">"数据添加成功,受影响的行数为:"</span>.$result; </span><span style="color: black;">} <span style="color: black;">catch</span> (PDOException $e){</span><span style="color: black;"> <span style="color: black;">die</span> (<span style="color: black;">"Error!:"</span>.$e->getMessage().<span style="color: black;"><br/></span>);</span><span style="color: black;">}</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2).query()<span style="color: black;">办法</span> 用于返回执行<span style="color: black;">查找</span>后的结果集</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">query()<span style="color: black;">办法</span>用于返回执行<span style="color: black;">查找</span>后的结果集:</p>实例:
<span style="color: black;"><span style="color: black;">try</span>{</span><span style="color: black;"> $pdo = <span style="color: black;">new</span> PDO($dsn,$user,$pass);</span><span style="color: black;"> $sql = <span style="color: black;">select * from tb_pdo_mysql</span>;</span><span style="color: black;"> $result = $pdo->query($sql);</span><span style="color: black;"> <span style="color: black;">foreach</span> ($result <span style="color: black;">as</span> $row) {</span><span style="color: black;"> <span style="color: black;">echo</span> <span style="color: black;">"<tr align=center>"</span>.<span style="color: black;">"<td>"</span>.$row[<span style="color: black;">id</span>].<span style="color: black;">"</td>"</span>;</span><span style="color: black;"> <span style="color: black;">echo</span> <span style="color: black;">"<td>"</span>.$row[<span style="color: black;">pdo_type</span>].<span style="color: black;">"</td>"</span>;</span><span style="color: black;"> <span style="color: black;">echo</span> <span style="color: black;">"<td>"</span>.$row[<span style="color: black;">database_name</span>].<span style="color: black;">"</td>"</span>;</span><span style="color: black;"> <span style="color: black;">echo</span> <span style="color: black;">"<td>"</span>.$row[<span style="color: black;">dates</span>].<span style="color: black;">"</td>"</span>.<span style="color: black;">"</tr>"</span>;</span><span style="color: black;"> }</span><span style="color: black;">} <span style="color: black;">catch</span> (PDOException $e){</span><span style="color: black;"> <span style="color: black;">die</span> (<span style="color: black;">"Error!:"</span>.$e->getMessage().<span style="color: black;"><br/></span>);</span><span style="color: black;">}</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">*声明:本文于网络整理,版权归原作者所有,如<span style="color: black;">源自</span>信息有误或侵犯权益,请联系<span style="color: black;">咱们</span>删除或授权事宜。</span></p><img src="https://mmbiz.qpic.cn/mmbiz_gif/LFP9SpGv0PFonacOKxEOcN8bHYq5Hyn870JicAH1y51rLbO3fF46aibXLRXleDvibc7q9hIfZMj6QDKLVAzHibdBXw/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1" style="width: 50%; margin-bottom: 20px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/LFP9SpGv0PHn7RHfichibiaAhibKghiacicKX4LHaZXVjaibdzDdExc4DtEoXZAuDSA3SCicQ56Hkgq3AyODWtnibWh3w9g/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">END<img src="https://mmbiz.qpic.cn/mmbiz_jpg/LFP9SpGv0PHn7RHfichibiaAhibKghiacicKX4CLibX2vEufeljiadjkODkRPSzLXjMYXfq1U6BdYNMicUhEhe2EXlAr7VA/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;">PHP开源社区<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">扫描关注 进入”PHP资料“</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">免费获取进阶</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">面试、文档、视频资源</p><strong style="color: blue;">点击“查看原文”获取<span style="color: black;">更加多</span></strong>
你的见解独到,让我受益匪浅,期待更多交流。 一看到楼主的气势,我就觉得楼主同在社区里灌水。
页:
[1]