PHP tips for using the PDO abstraction layer to get query results

Source: Internet
Author: User
Tags psql redis cluster
This article mainly introduces the use of the PDO abstraction layer to obtain query results, combined with examples of PHP using the PDO abstraction layer to obtain query results of three common methods and related functions of the use of skills, the need for friends can refer to the next

This example describes how PHP uses the PDO abstraction layer to get query results. Share to everyone for your reference, as follows:

PHP uses the PDO abstraction layer to get query results, there are three main ways:

(1) pdo::query () query.

Look at the following PHP code:

<?php//pdo::query () query $psql= "SELECT * from user", $res = $db->query ($psql); $res->setfetchmode (Pdo::fetch_num ); Digital Index mode while ($row = $res->fetch ()) {Print_r ($row);}? >

(2) pdo->exec () Processing SQL

<?php//pdo->exec () Handles sql$psql= "INSERT into user (ID, username) VALUES (', ' Zhang San ')"; $db->setattribute (pdo::attr _errmode, pdo::errmode_exception); $res = $db->exec ($psql); Echo $res;? >

(3) PDO::p repare () Preprocessing execution query

<?php//pdo::p Repare () preprocessing executes the query $psql= "SELECT * from user", $res = $db->prepare ($psql), $res->execute (), while ($ row = $res->fetchall ()) {Print_r ($row);}? >

setAttribute()The method is to set properties, common parameters are as follows:

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

setFetchModemethod to set the type of return value that gets the result set, with the following common parameters:

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 ()

The above summarizes the following:

Query operations are mainly PDO::query() , PDO::exec() , PDO::prepare() .

PDO->query() -Process an SQL statement and return a "pdostatement"
PDO->exec() -Processes an SQL statement and returns the number of entries affected

PDO::prepare()It is primarily a preprocessing operation that requires a $rs->execute () to execute the SQL statement inside the preprocessing

Finally, two commonly used functions are introduced:

(1) fetchColumn() Gets the result of a field in the specified record, the default is the first field!

<?php$psql= "SELECT * from user"; $res = $db->query ($psql);//Gets the second field result in the specified record $col = $res->fetchcolumn (1); echo $ Col;? >

(2) fetchAll() get data from a result set and store it in an associative array

<?php$psql= "SELECT * from user", $res = $db->query ($psql), $res _arr = $res->fetchall ();p rint_r ($res _arr);? >

Articles you may be interested in:

PHP to compress image size and convert to JPG format example _php tips

Example of how PHP remembers state when searching _php tips

Laravel Project full steps to deploy a redis cluster using twemproxy PHP instance









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.