PHP uses PDO to connect to an Access database and cycle through data operations

Source: Internet
Author: User
This article mainly introduces the use of PDO to connect to access database and cycle display data operations, combined with examples of the form of a more detailed analysis of PHP using PDO Access database connection, query, execute SQL statements, preprocessing and other related operational skills and considerations, A friend you need can refer to the following

The examples in this article describe PHP's use of PDO to connect to an Access database and iterate over data operations. Share to everyone for your reference, as follows:

PDO connection and query:

try {$conn = new PDO ("Odbc:driver={microsoft Access Driver (*.mdb)};       Dbq= ". Realpath (" Mydatabase.mdb "))       or Die (" Link Error! ");//echo" Link succeeded! ";} catch (Pdoexception $e) {  echo $e->getmessage ();} $sql = "SELECT * from users";

1. foreach() Methods

foreach ($conn->query ($sql) as $row) {$row ["UserID"]; $row ["UserName"]; $row ["UserPassword"];}

2. while() methods

$rs = $conn->query ($sql), $rs->setfetchmode (Pdo::fetch_num), while ($row = $rs->fetch ()) {$row [0]; $row [1];$ ROW[2];}

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

(1) PDO::query() inquiry.

Look at the following PHP code:

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

(2) PDO->exec() working with SQL

<?php//pdo->exec () Handles Sql$db->setattribute (Pdo::attr_errmode, pdo::errmode_exception); $res = $db->exec ( "INSERT into User (Id,name) VALUES (', ' php dot-pass ')"); Echo $res; >

(3) PDO::prepare() preprocessing execution query

<?php//pdo::p repare () preprocessing execution Query $res = $db->prepare ("SELECT * from User"), $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 () -processes an SQL statement and returns 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$res = $db->query (' select * from user ');//Gets the second field result of the specified record $col = $res->fetchcolumn (1); Echo $col; >

(2) fetchAll() , fetching data from a result set and storing it in an associative array

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

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: 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.