当前位置:网站首页>使用 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
边栏推荐
- VIM usage
- Read integrity monitoring techniques for vision navigation systems
- Wonderful review | deepnova x iceberg meetup online "building a real-time data Lake based on iceberg"
- 59、螺旋矩阵(数组)
- Jerry sometimes finds that the memory has been tampered with, but there is no exception. How should he find it? [chapter]
- Pycharm
- 707、设计链表(链表)
- 【leetcode】199.二叉树的右视图
- Differences among restful, soap, RPC, SOA and microservices
- 主流手机分辨率与尺寸
猜你喜欢

UEditor之——图片上传组件大小4M的限制

Yarn resource scheduler

Notes on concurrent programming of vegetables (V) thread safety and lock solution

Yarn core parameter configuration

SQL Server cursor circular table data

ID number verification system based on visual structure - Raspberry implementation

STM32接电机驱动,杜邦线供电,然后反烧问题

Introduction to data analysis 𞓜 kaggle Titanic mission (IV) - > data cleaning and feature processing

Initial exploration of NVIDIA's latest 3D reconstruction technology instant NGP

SQL Server recursive query of superior and subordinate
随机推荐
997. Square of ordered array (array)
Cve-2019-0708 vulnerability exploitation of secondary vocational network security 2022 national competition
Contact between domain name and IP address
主流手机分辨率与尺寸
A diary of dishes | 238 Product of arrays other than itself
206、反转链表(链表)
我的创作纪念日
Comparison and practice of prototype design of knowledge service app
202. Happy number
使用zerotier让异地设备组局域网
349、两个数组的交集
SQL Server 游标循环表数据
Learning Notes 6 - Summary of several deep learning convolutional neural networks
MapReduce compression
【leetcode】102. Sequence traversal of binary tree
Jerry sometimes finds that the memory has been tampered with, but there is no exception. How should he find it? [chapter]
MapReduce core and foundation demo
VIM usage
142. Circular linked list||
Resolution and size of mainstream mobile phones