当前位置:网站首页>使用 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
边栏推荐
- [Niuke challenge 47] C. conditions (BitSet acceleration Floyd)
- mysql同一个表中相同数据怎么合并
- What about Jerry's stack overflow? [chapter]
- VIM + ctags + cscope development environment construction guide
- Introduction to data analysis 𞓜 kaggle Titanic mission (IV) - > data cleaning and feature processing
- Understand the key points of complement
- 图像处理——噪声小记
- 【leetcode】107. Sequence traversal of binary tree II
- 202、快乐数
- Read integrity monitoring techniques for vision navigation systems - 4 multiple faults in vision system
猜你喜欢
得到知识服务app原型设计比较与实践
Yarn core parameter configuration
Idea - indexing or scanning files to index every time you start
Let the LAN group use the remote device
Example of pop-up task progress bar function based on pyqt5
解决方案架构师的小锦囊 - 架构图的 5 种类型
The courses bought at a high price are open! PHPer data sharing
Ueditor -- limitation of 4m size of image upload component
How to quickly download vscode
Chapter 120 SQL function round
随机推荐
解决方案架构师的小锦囊 - 架构图的 5 种类型
SQL Server 递归查询上下级
Contact between domain name and IP address
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
Construction and traversal of binary tree
Wonderful review | deepnova x iceberg meetup online "building a real-time data Lake based on iceberg"
Arbitrary file reading vulnerability exploitation Guide
关于JUC三大常用辅助类
Solution architect's small bag - 5 types of architecture diagrams
Diary of dishes | Blue Bridge Cup - hexadecimal to octal (hand torn version) with hexadecimal conversion notes
Hikvision face to face summary
Full stack cross compilation x86 completion process experience sharing
Reading integrity monitoring techniques for vision navigation systems - 3 background
209. Subarray with the smallest length (array)
24. Exchange the nodes in the linked list (linked list)
Net start MySQL MySQL service is starting MySQL service failed to start. The service did not report any errors.
How to bind a process to a specified CPU
SWAT—Samba WEB管理工具介绍
997. Square of ordered array (array)