Introduction to PDO, Flower thousand bones Synopsis _php Tutorial

Source: Internet
Author: User

Introduction to PDO, flowers thousand bones synopsis


PHP link database After six months need to replace MySQL for cluster mode or have the money to upgrade the ORACL database, when the changes are quite large, high cost. If you use PDO before, it's easy to meet the problem again.

To turn on PDO:

Open the php.ini file, you will need to open the DLL extension in front of the ";" Remove.

; Extension=php_pdo_firebird.dll

; Extension=php_pdo_mysql.dll

; Extension=php_pdo_oci.dll

; Extension=php_pdo_odbc.dll

Test can be used

Open Php.info () to see:

Using syntax

$db->setattribute (Pdo::attr_case, PDO::case_upper); $rs $db->query ("SELECT * from foo"); $rs->setfetchmode (PDO::fetch_assoc); $result _arr $rs-Fetchall (); Print_r ($result _arr);

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 name to uppercase

Setfetchmode ():

PDO::FETCH_ASSOC--Associative array form
Pdo::fetch_num--Digital Index array form
Pdo::fetch_both--both array form, this is the default
Pdo::fetch_obj-In the form of an object, similar to the previous mysql_fetch_object ()

Exception handling:

Try{    $db=NewPDO ($dsn,$user,$passwd);}Catch(pdoexception$e){    Print $e->getmessage ();//Return exception Information    Print $e->getcode ();//return Exception Code    Print $e->getfile ();//returns the file name of the exception that occurred    Print $e->getline ();//returns the line number of the code where the exception occurred    Print $e->gettrace ();//backtrace () array    Print $e->gettraceasstring ();//Gettrace () information that has been rasterized into a string}$count=$db-exec("INSERT into Info_u set name,nickname = ' Hefe ', job=1;");Print $db-ErrorCode ();Print_r($db-errorinfo ());--------------------------------------------------

[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

Array (    [0] = = 42s22    [1] = 1054    [2]= = ' name ' in ' Field List ' ) 

In the new link, exception handling is performed with Pdoexception (); Exception handling using ErrorInfo () and errorcode () when executing exection ();

ErrorCode () returns:

00000//Perform normal

1054//unknown column ' x ' in ' Field List ' fields X does not appear in the field

1110//field appears two times two times

1062//duplicate entry ' X ' for key ' PRIMARY ' repeating primary key

......

http://www.bkjia.com/PHPjc/1048737.html www.bkjia.com true http://www.bkjia.com/PHPjc/1048737.html techarticle PDO introduction, Flowers thousand bones synopsis PHP link database after six months need to replace MySQL for cluster mode or have the money to upgrade the ORACL database, when the changes are quite large, high cost. If the ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.