What will be connection string to Access database file with PHP

吃可爱长大的小学妹 提交于 2019-12-20 05:13:05

问题


I installed WAMP, I have access database file in project folder, but don't have installed Access on my computer.

Can I read and update Access file with PHP even I don't have installed Access?

And what will be connection string to Access database file?

I really need help with this.


回答1:


All you need is the PHP api for ODBC. Here is the example from the docs itself:

<?php
// Microsoft SQL Server using the SQL Native Client 10.0 ODBC Driver - allows connection to SQL 7, 2000, 2005 and 2008
$connection = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server;Database=$database;", $user, $password);

// Microsoft Access
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $user, $password);

// Microsoft Excel
$excelFile = realpath('C:/ExcelData.xls');
$excelDir = dirname($excelFile);
$connection = odbc_connect("Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=$excelFile;DefaultDir=$excelDir" , '', '');
?>



回答2:


// Microsoft Access

  1. Open the Administrative Tools icon in your Control Panel.
  2. Double-click on the Data Sources (ODBC) icon inside.
  3. Choose the System DSN tab.
  4. Click on Add in the System DSN tab.
  5. Select the Microsoft Access Driver.
  6. Click Finish.
  7. In the next screen, click Select to locate the database.
  8. Give the database a Data Source Name (DSN).
  9. Click OK.

    $dsn='database.accdb';
    $username='';
    $password='';
    $connect=odbc_connect($dsn, $username, $password);
    



回答3:


I'v found this link with a tutorial on how to do it. Be careful that things work differently in windows and UNIX environment, but since you are using a WAMP you should have no problems




回答4:


<?php

$db = $_SERVER["DOCUMENT_ROOT"] ."/AccessDatabase/reg.accdb"; //AccessDatabase is folder in htdocs where the database is store 
if (!file_exists($db))
{
       die("No database file.");
}

$dbNew = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=$db; Uid=; Pwd=;");
$sql = "select * from reg"; //reg is table name
$rs = $dbNew->query($sql);

while($result = $rs->fetch())
{
     echo $result[0].": ".$result[1].": ".$result[2]."<br />";
} 


?>

If u got error like pdo ODBC Drivers not installed just go to php.ini and find extension = pdo_ODBC Driver and remove the comment(;) after that restart the apache



来源:https://stackoverflow.com/questions/6328554/what-will-be-connection-string-to-access-database-file-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!