Php pdo Basics

Source: Internet
Author: User

PDO is a "database access abstraction layer" that unifies access interfaces of various databases. Compared with MySQL and mysqli function libraries, PDO makes cross-database use more friendly; compared with ADODB and mdb2, PDO is more efficient. At present, there is a long way to implement the "database abstraction layer". Using the "database access abstraction layer" such as PDO is a good choice. <Br/> PdO-> begintransaction ()-indicates the rollback start point <br/> PdO-> commit ()-indicates the rollback end point, run the SQL statement <br/> PdO->__ construct ()-create a PDO-Linked database instance <br/> PdO-> errorcode () -Get Error Code <br/> PdO-> errorinfo ()-Get error information <br/> PdO-> exec ()-process an SQL statement, and return the number of affected entries <br/> PdO-> getattribute ()-Get the attribute of a "database connection object" <br/> PdO-> getavailabledrivers () -Get a valid PDO drive name <br/> PdO-> lastinsertid ()-Get the primary key value of the last data entry written <br/> PdO-> prepare () -generate a "query object" <br/> PdO-> que Ry ()-process an SQL statement and return a "pdostatement" <br/> PdO-> quote () -quote a string in an SQL statement <br/> PdO-> rollback ()-Execute rollback <br/> PdO-> setattribute () -Set attributes for a "database connection object" <br/> pdostatement-> bindcolumn ()-bind a column to a PHP variable <br/> pdostatement-> bindparam () -binds a parameter to the specified variable name <br/> pdostatement-> bindvalue ()-binds a value to a parameter <br/> pdostatement-> closecursor ()-closes Cursor, enabling the statement to be executed again. <br/> pdostatement-> columncount ()-returns the number of columns in the result set <br/> pdostatement-> errorcode () -Fetch the sqlstate associated with the last operation on the statement handle <br/> pdostatement-> errorinfo () -Fetch extended error information associated with the last operation on the statement handle <br/> pdostatement-> execute ()-Executes a prepared statement <br/> pdostatement-> fetch ()-fetches the next row from a result set <br/> pdostatement-> fetchall () -returns an array containing all of the result set rows <br/> pdostatement-> fetchcolumn () -returns a single column from the next row of a result set <br/> pdostatement-> fetchobject ()-fetches the next row and returns it as an object. <br/> pdostatement-> getattribute () -Retrieve a statement attribute <br/> pdostatement-> getcolumnmeta ()-returns metadata for a column in a result set <br/> pdostatement-> nextrowset () -advances to the next rowset in a multi-rowset statement handle <br/> pdostatement-> rowcount () -returns the number of rows affected by the last SQL statement <br/> pdostatement-> setattribute ()-set a statement attribute <br/> pdostatement-> setfetchmod E ()-set the default fetch mode for this statement <br/> from the function list, we can see that the operation is based on different objects, "PDO" indicates a database connection object (generated by new PDO), and "pdostatement" indicates a query object (generated by PDO-> query) or a result set object (PDO-> prepare ). <Br/> in an example of "database connection object", "PDO" is returned: <br/> <? Php <br/> $ DBH = new PDO ('mysql: host = localhost; dbname = test', 'root', ''); <br/>?> <Br/> for an example of "query object", "pdostatement" is returned: <br/> <? Php <br/> $ SQL = "insert into 'test'. 'table' ('name', 'age') values (?, ?); "; <Br/> $ stmt = $ DBH-> prepare ($ SQL); <br/>?> <Br/> for an example of "result set object", "pdostatement" is returned: <br/> <? Php <br/> $ SQL = "select * From 'table' where 'name' = 'samon '"; <br/> $ stmt = $ DBH-> query ($ SQL); <br/>?> <Br/> Generate a table in the test database and write some data: <br/> <? Php <br/> $ DBH = new PDO ('mysql: host = localhost; dbname = test', 'root ',''); <br/> $ DBH-> query ('set names utf8; '); <br/> $ SQL = "<br/> drop table if exists 'table '; <br/> Create Table 'test '. 'table' (<br/> 'id' int (10) Not null auto_increment primary key, <br/> 'name' varchar (255) not null, <br/> 'age' varchar (255) not null <br/>) engine = InnoDB default charset = utf8; <br/> insert into 'test '. 'table' ('Name', 'age') values ('samon', '27'); <br/> insert into 'test '. 'table' ('name', 'age') values ('Leo ', '26'); <br/> insert into 'test '. 'table' ('name', 'age') values ('ben', '28'); <br/> "; <br/> $ DBH-> query ($ SQL); <br/>?> <Br/> the PDO-> exec () method processes an SQL statement and returns the number of affected items. <Br/> the PDO-> query () method processes an SQL statement and returns a "pdostatement ". <Br/> the function of the PDO-> prepare () method is to generate a "query object ". <Br/> the PDO-> lastinsertid () method is used to obtain the primary key value of the last written data. <Br/> <? Php <br/> $ DBH = new PDO ('mysql: host = localhost; dbname = test', 'root ',''); <br/> $ DBH-> query ('set names utf8; '); <br/> $ SQL = "insert into 'test '. 'table' ('name', 'age') values ('mick', 22); "; <br/> $ DBH-> query ($ SQL ); <br/> $ lid = $ DBH-> lastinsertid ($ SQL); <br/> print_r ($ lid); // display the primary key value <br/>?> <Br/> PdO-> quote () is used to add quotation marks to strings in an SQL statement. The PDO-> quote () method has two parameters. The first parameter is a string, and the second parameter represents the parameter type. <Br/> the PDO-> getattribute () method is used to obtain the attribute of a "database connection object. The PDO-> setattribute () method sets attributes for a "database connection object. <Br/> <? Php <br/> // modify the default error display level <br/> $ DBH-> setattribute (PDO: attr_errmode, PDO: errmode_warning); <br/>?> <Br/> attribute list: <br/> PDO: param_bool <br/> indicates a boolean type. <br/> PDO :: param_null <br/> indicates the null type in an SQL statement <br/> PDO: param_int <br/> indicates the integer type in an SQL statement <br/> PDO :: param_str <br/> indicates SQL char and varchar type in an SQL statement <br/> PDO: param_lob <br/> indicates the large object type in an SQL statement <br/> PDO :: param_stmt <br/> indicates the recordset type in an SQL statement, which is not supported yet <br/> PDO: param_input_output <br/> specifies that the parameter is an inout parameter for a stored procedu Re. you must bitwise-or this value with an explicit PDO: Param _ * data type. <br/> PDO: fetch_lazy <br/> returns the result of each row as an object. <br/> PDO :: fetch_assoc <br/> returns only the result set of the query with the key value as the underlying value. Only one data with the same name is returned. <br/> PDO :: fetch_named <br/> returns only the result set of the query whose names are the same as the key value. data with the same name is returned in an array. <br/> PDO :: fetch_num <br/> returns only the result set of the query that uses numbers as the lower mark. <br/> PDO :: fetch_both <br/> both return the result set of the query with the key value and number as the bottom mark <br/> PDO :: fetch_obj <br/> returns the result set as an object <br/> PDO: fetch_bound <br/> Use the value bound to pdostatement: bindparam () and pdostatement: bindcolumn () as the variable name to assign values and return <br/> PDO :: fetch_column <br/> indicates that only one column in The result set is returned. <br/> PDO: fetch_class <br/> indicates that the result set is returned in the form of a class. <br/> PDO:: fetch_into <br/> indicates that data is merged into an existing class and returned <br/> PDO: fetch_func <br/> PDO: fetch_group <br/> PDO:: fetch_unique <br/> PDO: fetch_key_pair <br/> return the result set in the form of the first key value table and the following number table <br/> PDO :: fetch_classtype <br/> PDO: fetch_serialize <br/> indicates that data is merged into an existing class and serialized and returned. <Br/> PDO: fetch_props_late <br/> available since PHP 5.2.0 <br/> PDO: attr_autocommit <br/> when it is set to true, PDO automatically stops accepting delegation and starts executing <br/> PDO: attr_prefetch <br/> to set the data size obtained by the application in advance, not all databases support <br/> PDO: attr_timeout <br/> set the database connection timeout value <br/> PDO :: attr_errmode <br/> set the error handling mode <br/> PDO: attr_server_version <br/> Read-only attribute, indicating the version of the server database connected to PDO <br/> PDO :: attr_client_version <br/> Read-only attribute, indicating the PDO driver version of the Client Connected to PDO <br/> PDO: ATTR _ Server_info <br/> Read-only attribute, indicating the meta information of the server to which the PDO connects <br/> PDO: attr_connection_status <br/> PDO: attr_case <br/> through PDO :: the content in case _ * operates on the column form <br/> PDO: attr_cursor_name <br/> get or set the pointer name <br/> PDO :: attr_cursor <br/> sets the pointer type. PDO currently supports PDO: cursor_fwdonly and PDO: cursor_fwdonly. <br/> PDO :: attr_driver_name <br/> returns the name of the PDO driver used <br/> PDO: attr_oracle_nulls <br/> converts the returned null string to the SQL null <br/> PDO :: attr_persistent <br/> get an existing connection <B R/> PDO: attr_statement_class <br/> PDO: attr_fetch_catalog_names <br/> in the returned result set, use the custom directory name to replace the field name. <Br/> PDO: attr_fetch_table_names <br/> in the returned result set, use the custom table name instead of the field name. <Br/> PDO: Drawing <br/> PDO: attr_max_column_len <br/> PDO: attr_default_fetch_mode <br/> available since PHP 5.2.0 <br/> PDO :: attr_emulate_prepares <br/> available since PHP 5.1.3. <br/> PDO: errmode_silent <br/> no error message is reported when an error occurs. The default value is <br/> PDO :: errmode_warning <br/> a PHP e_warning message is sent when an error occurs. <br/> PDO: errmode_exception <br/> A pdoexception is thrown when an error occurs. <br/> PDO :: case_natural <br/> default display format of the reply column <Br/> PDO: case_lower <br/> force column name in lower case <br/> PDO: case_upper <br/> force column name in upper case <br/> PDO :: null_natural <br/> PDO: null_empty_string <br/> PDO: null_to_string <br/> PDO: fetch_ori_next <br/> obtain the next row of data in the result set, valid only when the pointer function is available <br/> PDO: fetch_ori_prior <br/> obtains the data of the previous row in the result set. Valid only when the pointer function is available <br/> PDO :: fetch_ori_first <br/> obtain the first row of data in the result set, which is valid only when the pointer function is available <br/> PDO: fetch_ori_last <br/> to obtain the last row of data in the result set, valid only when the pointer function is available <br/> PDO: fetch_ori_abs <br/> Obtain the data of a row in the result set, which is only valid when the pointer function is available <br/> PDO: fetch_ori_rel <br/> to obtain the data of a row after the current row in the result set, valid only when the pointer function is available <br/> PDO: cursor_fwdonly <br/> creates a pointer operation object that can only be backward <br/> PDO :: cursor_scroll <br/> creates a pointer operation object and transmits the content in PDO: fetch_ori _ * to control the result set. <br/> PDO: err_none (string) <br/> set the error message when no error occurs <br/> PDO: param_evt_alloc <br/> allocation event <br/> PDO :: param_evt_free <br/> deallocation event <br/> PDO: param_evt_exec_pre <br/> event triggered PRI Or to execution of a prepared statement. <br/> PDO: param_evt_exec_post <br/> event triggered subsequent to execution of a prepared statement. <br/> PDO: param_evt_fetch_pre <br/> event triggered prior to fetching a result from a resultset. <br/> PDO: param_evt_fetch_post <br/> event triggered subsequent to fetching a result from a resultset. <br/> PDO: param_evt_normalize <br/> event triggered Bound parameter registration allowing the driver to normalize the parameter name. <br/> PdO-> the getavailabledrivers () method is used to obtain a valid PDO drive name. <Br/> the function of the PDO-> errorinfo () method to obtain error information. The function of the PDO-> errorcode () method is to obtain error codes. <Br/> <? Php <br/> $ DBH = new PDO ('mysql: host = localhost; dbname = test', 'root ',''); <br/> $ DBH-> query ('set names utf8; '); <br/> $ SQL = "insert into 'test '. 'table' ('name', 'ageeeeeee') values ('mick', 22); "; <br/> $ DBH-> query ($ SQL ); <br/> print_r ($ DBH-> errorinfo ()); <br/>/** <br/> * array <br/> * (<br/> * [0] => 42s22 <br/> * [1] => 1054 <br/> * [2] => unknown column 'ageeeeeee' in 'field list' <br/> *) <br/> */<br/>?> <Br/> the PDO->__ construct () method is used to create a PDO-Linked database instance. <Br/> PdO-> begintransaction (), PDO-> commit (), PDO-> rollback () are used together to support the rollback function. The PDO-> begintransaction () method indicates the start point, the PDO-> commit () method indicates the end point of the rollback, and executes the SQL statement. The PDO-> rollback () method performs the rollback. <Br/> <? Php <br/> try {<br/> $ DBH = new PDO ('mysql: host = localhost; dbname = test', 'root ',''); <br/> $ DBH-> query ('set names utf8; '); <br/> $ DBH-> setattribute (PDO: attr_errmode, PDO: errmode_exception ); <br/> $ DBH-> begintransaction (); <br/> $ DBH-> exec ("insert into 'test '. 'table' ('name', 'age') values ('mick', 22 );"); <br/> $ DBH-> exec ("insert into 'test '. 'table' ('name', 'age') values ('lily', 29); "); <br/> $ DB H-> exec ("insert into 'test '. 'table' ('name', 'age') values ('Susan ', 21); "); <br/> $ DBH-> commit (); </P> <p >}catch (exception $ e) {<br/> $ DBH-> rollback (); <br/> echo "failed :". $ e-> getmessage (); <br/>}< br/>?>

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.