PHP uses pdo to connect to the access database and loop through the data manipulation sample

  • 2021-10-15 10:04:17
  • OfStack

This article illustrates how PHP uses pdo to connect to an access database and display data operations in a loop. Share it 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 successful! ";
}
catch(PDOException $e){
  echo $e->getMessage();
}
$sql = "select * from users";

1. foreach() Method


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

2. while() Method


$rs = $conn->query($sql);
$rs->setFetchMode(PDO::FETCH_NUM);
while($row=$rs->fetch()){
$row[0];
$row[1];
$row[2];
}

php uses PDO abstraction layer to obtain query results in three main ways:

(1) PDO::query() Query.

Look at the following php code:


<?php
//PDO::query() Query 
$res = $db->query('select * from user');
$res->setFetchMode(PDO::FETCH_NUM); // Digital indexing mode 
while ($row = $res->fetch()){
print_r($row);
}
?>

(2) PDO->exec() Processing sql


<?php
//PDO->exec() Deal with sql
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$res = $db->exec("insert into user(id,name) values('','php Point-to-point communication ')");
echo $res;
?>

(3) PDO::prepare() Preprocessing executes queries


<?php
//PDO::prepare() Preprocessing executes queries 
$res = $db->prepare("select * from user");
$res->execute();
while ($row = $res->fetchAll()) {
print_r($row);
}
?>

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

PDO:: CASE_LOWER--Force column names to be lowercase
PDO:: CASE_NATURAL--Column name as original
PDO:: CASE_UPPER--Force column names to uppercase

setFetchMode Method to set the type of return value to get the result set. The common parameters are as follows:

PDO:: FETCH_ASSOC--associative array form
PDO:: FETCH_NUM--Digital index array form
PDO:: FETCH_BOTH--Both in array form, which is the default
PDO:: FETCH_OBJ--In the form of an object, similar to the previous mysql_fetch_object ()

The above is summarized as follows:

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

PDO- > query ()--Processes 1 SQL statement and returns 1 "PDOStatement"
PDO- > exec ()--Processes 1 SQL statement and returns the number of entries affected
PDO::prepare() It is mainly preprocessing operation, which needs to pass $rs- > execute () to execute the SQL statement in the preprocessing

Finally, two commonly used functions are introduced:

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


<?php
$res = $db->query('select * from user');
// Gets the number in the specified record 2 Field results 
$col = $res->fetchColumn(1);
echo $col;
?>

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


<?php
$res = $db->query('select * from user');
$res_arr =$res->fetchAll();
print_r($res_arr);
?>

For more readers interested in PHP related content, please check the topics on this site: "Summary of PHP Database Operation Skills Based on pdo", "Summary of php+Oracle Database Programming Skills", "Encyclopedia of PHP+MongoDB Database Operation Skills", "Introduction to php Object-Oriented Programming", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: