当前位置:网站首页>Microsoft Access database using PHP PDO ODBC sample
Microsoft Access database using PHP PDO ODBC sample
2022-04-23 10:56:00 【allway2】
In this tutorial , We'll see a on how to use PDO The library is connected to MS Access Database PHP Code example .
<?php
echo '<pre>';
print_r(PDO::getAvailableDrivers());
echo '</pre>';
?>
Before we move on , Please make sure PDO Library enabled . Add the above code to php File and run in the browser . It will tell you which libraries are currently active

If it is not activated , Please edit PHP.ini File and find ODBC Of PDO That's ok

complete PHP PDO Code query adventure works MS Access database
<?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://yzsam.com/2022/04/202204231051097982.html
边栏推荐
- 精彩回顾|「源」来如此 第六期 - 开源经济与产业投资
- Is the pointer symbol of C language close to variable type or variable name?
- 全栈交叉编译X86完成过程经验分享
- Deploy jar package
- Intuitive understanding entropy
- Arbitrary file reading vulnerability exploitation Guide
- JDBC – PreparedStatement – 如何设置 Null 值?
- Problems of class in C # and database connection
- Visual common drawing (III) area map
- Image processing - Noise notes
猜你喜欢

A diary of dishes | 238 Product of arrays other than itself

SQL Server cursor circular table data

Intuitive understanding entropy

Learning note 5 - gradient explosion and gradient disappearance (k-fold cross verification)

/Can etc / shadow be cracked?

Detailed explanation of typora Grammar (I)

精彩回顾 | DEEPNOVA x Iceberg Meetup Online《基于Iceberg打造实时数据湖》

Swagger2 接口如何导入Postman

Solutions to common problems in visualization (IX) background color

Let the LAN group use the remote device
随机推荐
Construction and traversal of binary tree
Visual Road (XII) detailed explanation of collection class
景联文科技—专业数据标注公司和智能数据标注平台
MBA-day5數學-應用題-工程問題
CentOS/Linux安装MySQL
JDBC – PreparedStatement – 如何设置 Null 值?
SSH利用私钥无密钥连接服务器踩坑实录
Reading integrity monitoring techniques for vision navigation systems - 5 Results
得到知识服务app原型设计比较与实践
【leetcode】199. Right view of binary tree
RESTful和SOAP的区别
Contact between domain name and IP address
GO接口使用
Leetcode22: bracket generation
Learning notes 7-depth neural network optimization
最强日期正则表达式
Example of pop-up task progress bar function based on pyqt5
Wonderful review | deepnova x iceberg meetup online "building a real-time data Lake based on iceberg"
Introduction to wechat applet, development history, advantages of applet, application account, development tools, initial knowledge of wxml file and wxss file
The difference between restful and soap