当前位置:网站首页>使用 PHP PDO ODBC 示例的 Microsoft Access 数据库
使用 PHP PDO ODBC 示例的 Microsoft Access 数据库
2022-04-23 10:51:00 【allway2】
在本教程中,我们将看到一个关于如何使用 PDO 库连接到 MS Access 数据库的 PHP 代码示例。
<?php
echo '<pre>';
print_r(PDO::getAvailableDrivers());
echo '</pre>';
?>
在我们继续之前,请确保 PDO 库已启用。将上述代码添加到 php 文件中并在浏览器中运行。它会告诉你哪些库当前处于活动状态

如果它未激活,请编辑 PHP.ini 文件并找到 ODBC 的 PDO 行

完整的PHP PDO代码查询冒险作品MS访问数据库
<?php
//test_mysql.php?db=infocaptor_server&host=localhost&port=3306&user=root&pass=root
// Location to file
$db = 'c:\samples\AdventureWorks.accdb';
$db_param["name"]=$db;
$sql = 'select * from "Purchasing_Vendor"';
if(!file_exists($db)){
die('Error finding access database');
}
echo "<br>Testing Access PDO";
$content=access_result_pdo_x($db_param,$sql);
print_r($content);
exit;
// Connection to ms access
$db = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=".$db.";Uid=; Pwd=;");
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
if ($db)
echo "<br>PDO connection success\n";
else
echo "<br>pdo connection failed\n";
try{
$result = $db->query($sql);
$row = $result->fetchAll(PDO::FETCH_ASSOC);
print_r($row);
}catch(PDOExepction $e){
echo $e->getMessage();
}
function access_result_pdo_x($db_param,$sql)
{
try
{
echo "Calling access_result_pdo_x<br>";
if(!file_exists($db_param["name"]))
{
throw new RuntimeException('Access Database file path is incorrect or file does not exist.');
}
try
{
$cnnt_str="odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=".$db_param["name"].";Uid=; Pwd=;";
$db = new PDO($cnnt_str);
echo $cnnt_str;
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch (exception $e)
{
print_r($e,true);
$content["error_flag"]="true";
$content["error_mesg"]="access connection error ". $e->getMessage() ;
$content["legends"]=array("Error");
$content["data"]=array(0);
return $content;
}
//run the query
try
{
$stmt=$db->query($sql);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
}
catch (exception $e)
{
$content["error_flag"]="true";
$err=$db->errorInfo();
$content["error_mesg"]="access query error ".$e->getMessage();
$content["legends"]=array("Error");
$content["data"]=array(0);
return $content;
}
// Loop to handle all results
$row_id=0;
$column_names=array();
$column_types=array();
$col_data=array();
$row_limit=$db_param['row_limit'];
if (!isset($row_limit) ) $row_limit=10;
while( ($row = $stmt->fetch()) && $row_id<$row_limit)
{
//$row_data[$row_id]=array();
// each key and value can be examined individually as well
$col_id=0;
//clog(2,print_r($row,true));
foreach($row as $key => $value)
{
if (is_int($key)) continue;
// clog(2,"key = {$key} val = {$value}");
$col_data[$col_id][$row_id]=utf8_encode($value);
if ($row_id==0)
{
//$col_meta=$stmt->getColumnMeta($col_id);
//xlog(0,"mysql column type =".$col_meta["native_type"]);
array_push($column_names,utf8_encode($key)); //just capture column names only once
array_push($column_types,"VARCHAR");
}
$col_id++;
//array_push($row_data[$row_id],$value);
// print "<br>Key: " . $key . " Value: " . $value;
}
$row_id++;
}
if ($col_id==0)
{
$column_names[]="No Data";
$column_types[]="VARCHAR";
$col_data[0][0]="no data";
}
$content["legends"]=$column_names;
$content["columnTypes"]=$column_types;
$content["data"]=$col_data;
return $content;
}
catch (exception $e)
{
$content["legends"]=array("Error");
$content["data"]=array(0);
$content["error_flag"]="true";
$content["error_mesg"]="fetch error ".print_r($e,true);
return $content;
}
}
?>
版权声明
本文为[allway2]所创,转载请带上原文链接,感谢
https://blog.csdn.net/allway2/article/details/124360584
边栏推荐
- MapReduce core and foundation demo
- C language - custom type
- 高价买来的课程,公开了!phper资料分享
- Wonderful review | deepnova x iceberg meetup online "building a real-time data Lake based on iceberg"
- Detailed explanation of MapReduce calculation process
- LeetCode 1249. Minimum remove to make valid parents - FB high frequency question 1
- Derivation and regularization
- 206、反转链表(链表)
- 202、快乐数
- 使用zerotier让异地设备组局域网
猜你喜欢

Deploy jar package

The courses bought at a high price are open! PHPer data sharing

精彩回顾|「源」来如此 第六期 - 开源经济与产业投资

How can swagger2 custom parameter annotations not be displayed

SQL Server 递归查询上下级

Chapter 120 SQL function round

Jinglianwen technology - professional data annotation company and intelligent data annotation platform

Cve-2019-0708 vulnerability exploitation of secondary vocational network security 2022 national competition

Source insight 4.0 FAQs

Charles function introduction and use tutorial
随机推荐
部署jar包
1. Sum of two numbers (hash table)
Reading integrity monitoring techniques for vision navigation systems - 3 background
【leetcode】102.二叉树的层序遍历
Can Jerry's AES 256bit [chapter]
Solutions to common problems in visualization (VIII) solutions to problems in shared drawing area
【leetcode】199. Right view of binary tree
VScode
206. Reverse linked list (linked list)
Detailed explanation of MapReduce calculation process
Example of pop-up task progress bar function based on pyqt5
Source insight 4.0 FAQs
C language - custom type
Learning note 5 - gradient explosion and gradient disappearance (k-fold cross verification)
The courses bought at a high price are open! PHPer data sharing
HuggingFace
Differences among restful, soap, RPC, SOA and microservices
209、长度最小的子数组(数组)
Solution architect's small bag - 5 types of architecture diagrams
C#和数据库连接中类的问题