PDO简介,花千骨剧情简介_PHP教程

WBOY
Lepaskan: 2016-07-13 09:44:44
asal
937 orang telah melayarinya

PDO简介,花千骨剧情简介

php链接数据库 半年后需要更换mysql为集群模式或者有钱了升级oracl数据库,这时的改动相当大,成本高。如果再之前使用PDO,之后再遇见这样的问题就很轻松。


开启PDO:

打开php.ini文件,将需要打开的dll扩展件前面的“;”去掉。

;extension=php_pdo_firebird.dll

;extension=php_pdo_mysql.dll

;extension=php_pdo_oci.dll

;extension=php_pdo_odbc.dll


测试能否使用

打开php.info()查看到: 


使用语法

<span>$db</span>->setAttribute(PDO::ATTR_CASE, PDO::<span>CASE_UPPER);
</span><span>$rs</span> = <span>$db</span>->query("SELECT * FROM foo"<span>);
</span><span>$rs</span>->setFetchMode(PDO::<span>FETCH_ASSOC);
</span><span>$result_arr</span> = <span>$rs</span>-><span>fetchAll();
</span><span>print_r</span>(<span>$result_arr</span>);
Salin selepas log masuk

setAttribute():

PDO::CASE_LOWER -- 强制列名是小写
PDO::CASE_NATURAL -- 列名按照原始的方式
PDO::CASE_UPPER -- 强制列名为大写

setFetchMode():

PDO::FETCH_ASSOC -- 关联数组形式
PDO::FETCH_NUM -- 数字索引数组形式
PDO::FETCH_BOTH -- 两者数组形式都有,这是缺省的
PDO::FETCH_OBJ -- 按照对象的形式,类似于以前的 mysql_fetch_object()

异常处理:

<span>try</span><span>{
    </span><span>$db</span> = <span>new</span> PDO(<span>$dsn</span>,<span>$user</span>,<span>$passwd</span><span>);
}</span><span>catch</span>(PDOException <span>$e</span><span>){
    </span><span>print</span> <span>$e</span>->getMessage();                <span>//</span><span> 返回异常信息</span>
    <span>print</span> <span>$e</span>->getCode();                   <span>//</span><span> 返回异常代码</span>
    <span>print</span> <span>$e</span>->getFile();                   <span>//</span><span> 返回发生异常的文件名</span>
    <span>print</span> <span>$e</span>->getLine();                   <span>//</span><span> 返回发生异常的代码行号</span>
    <span>print</span> <span>$e</span>->getTrace();                  <span>//</span><span> backtrace() 数组</span>
    <span>print</span> <span>$e</span>->getTraceAsString();          <span>//</span><span> 已格成化成字符串的 getTrace() 信息        </span>
<span>}
</span><span>$count</span> = <span>$db</span>-><span>exec</span>("insert into info_u set name,nickname ='hefe',job=1;"<span>);
</span><span>print</span> <span>$db</span>-><span>errorCode();
</span><span>print_r</span>(<span>$db</span>-><span>errorinfo());

</span>--------------------------------------------------
Salin selepas log masuk

[message:protected] => SQLSTATE[HY000] [1045] Access denied for user 'coffee'@'localhost' (using password: YES)

[string:Exception:private] =>

[code:protected] => 1045

[file:protected] => /alidata/www/webpage/signup.php

[line:protected] => 11

[trace:Exception:private] => Array

(

[0] => Array

(

[file] => /alidata/www/webpage/signup.php

[line] => 11

[function] => __construct

[class] => PDO

[type] => ->

[args] => Array

(

[0] => mysql:host=localhost;dbname=fengchao

[1] => coffee

[2] => coffe

)

)

)

[previous:Exception:private] =>

[errorInfo] =>

42S22

<em id="__mceDel"><span>Array</span><span>
(
    [</span>0] =><span> 42S22
    [</span>1] => 1054<span>
    [</span>2] =><span>
Unknown column </span>'name' in 'field list'<span>
 )</span></em>
Salin selepas log masuk

 在new新的链接时,异常处理用PDOException();在执行exection()时异常处理使用errorInfo()和errorCode();

errorCode()返回:

00000              //执行正常

1054               //Unknown column 'X' in 'field list' 字段X未出现在在字段中

1110               // 字段出现两次出现两次

1062               //Duplicate entry 'X' for key 'PRIMARY' 重复主键

 ......

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1048737.htmlTechArticlePDO简介,花千骨剧情简介 php链接数据库 半年后需要更换mysql为集群模式或者有钱了升级oracl数据库,这时的改动相当大,成本高。如果再之...
Label berkaitan:
pdo
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!