当前位置:网站首页>使用 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
边栏推荐
- 图像处理——噪声小记
- 707、设计链表(链表)
- 202. Happy number
- Image processing - Noise notes
- Esp32 learning - add folder to project
- 242、有效字母异位词(哈希表)
- Charles function introduction and use tutorial
- 使用zerotier让异地设备组局域网
- Latex usage
- Diary of dishes | Blue Bridge Cup - hexadecimal to octal (hand torn version) with hexadecimal conversion notes
猜你喜欢
Swagger2 自定义参数注解如何不显示
Introduction to data analysis 𞓜 kaggle Titanic mission (IV) - > data cleaning and feature processing
【leetcode】107.二叉树的层序遍历II
微信小程序简介、发展史、小程序的优点、申请账号、开发工具、初识wxml文件和wxss文件
精彩回顾 | DEEPNOVA x Iceberg Meetup Online《基于Iceberg打造实时数据湖》
GO接口使用
Charles 功能介绍和使用教程
How to quickly download vscode
Jinglianwen technology - professional data annotation company and intelligent data annotation platform
Notes on concurrent programming of vegetables (IX) asynchronous IO to realize concurrent crawler acceleration
随机推荐
STM32接电机驱动,杜邦线供电,然后反烧问题
VIM + ctags + cscope development environment construction guide
Net start MySQL MySQL service is starting MySQL service failed to start. The service did not report any errors.
Yarn resource scheduler
/Can etc / shadow be cracked?
任意文件读取漏洞 利用指南
707、设计链表(链表)
206、反转链表(链表)
Latex usage
997. Square of ordered array (array)
SQL server query database deadlock
Intuitive understanding entropy
JVM - common parameters
349、两个数组的交集
Charles 功能介绍和使用教程
Reading integrity monitoring techniques for vision navigation systems - 5 Results
[Niuke challenge 47] C. conditions (BitSet acceleration Floyd)
Introduction to wechat applet, development history, advantages of applet, application account, development tools, initial knowledge of wxml file and wxss file
【leetcode】102. Sequence traversal of binary tree
Source insight 4.0 FAQs