Home > Backend Development > PHP Tutorial > PDO introduction, Flower Thousand Bone plot introduction_PHP tutorial

PDO introduction, Flower Thousand Bone plot introduction_PHP tutorial

WBOY
Release: 2016-07-13 09:44:44
Original
985 people have browsed it

Introduction to PDO, plot introduction to Flower Thousand Bones

After half a year, the PHP link database needs to change mysql to cluster mode or upgrade the oracl database if you have money. The changes at this time are quite large and the cost high. If you used PDO before, it would be easy to encounter such problems later.


Open PDO:

Open the php.ini file and remove the ";" in front of the dll extension that needs to be opened.

;extension=php_pdo_firebird.dll

;extension=php_pdo_mysql.dll

;extension=php_pdo_oci.dll

;extension=php_pdo_odbc.dll


Test whether it can be used

Open php.info() and see:


Use syntax

<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>);
Copy after login

setAttribute():

PDO::CASE_LOWER -- Force column names to be lowercase
PDO::CASE_NATURAL -- Column names are in the original way
PDO::CASE_UPPER -- Force column names to be uppercase

setFetchMode():

PDO::FETCH_ASSOC -- Associative array form
PDO::FETCH_NUM -- Numeric index array form
PDO::FETCH_BOTH -- Both array forms are available, this is the default
PDO ::FETCH_OBJ -- In the form of an object, similar to the previous mysql_fetch_object()

Exception handling:

<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>--------------------------------------------------
Copy after login

[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

          (

                                                                                                                                                                                                                                         

                [line] => 11

                [function] => __construct

                [class] => PDO

                                                                                                                                                                                       

                [args] => Array

                (

[0] = & gt; mysql: host = localhost; dbname = fengchao

>

                                                                                                                                                                         

              )

        )  

)

[previous:Exception:private] =>

[errorInfo] =>

42S22

When creating a new link, use PDOException() for exception handling; when executing execution(), use errorInfo() and errorCode() for exception handling;

errorCode() returns: 00000                                                                                                                                                  

1054 //Unknown column 'X' in 'field list' Field X does not appear in the field
<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>
Copy after login

1110                                                                                                                                                                                                                                                                                

1062 //Duplicate entry 'X' for key 'PRIMARY' Duplicate primary key

http://www.bkjia.com/PHPjc/1048737.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/1048737.html

TechArticlePDO introduction, Flower Thousand Bones plot introduction PHP link database needs to change mysql to cluster mode after half a year or upgrade if you have money The changes to the oracle database at this time are quite large and the cost is high. If again...
Related labels:
pdo
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template