简体   繁体   English

将MS-Access数据库连接到PHP-DSN-less数据库时出错

[英]Error in connecting MS-Access database to PHP Server DSN-less

I am new to PHP as well as new to Ms-access database. 我是PHP的新手,也是Ms-access数据库的新手。 I am trying to connect php server to ms access database dsnless as i have to give this to my friend. 我正在尝试将php服务器连接到ms Access数据库dsnless,因为我必须将此发送给我的朋友。 My both php file and ms database file are in same directory and they both are going to stay on localhost only. 我的php文件和ms数据库文件都在同一目录中,并且它们都将仅保留在localhost上。 I am not able to understand the error. 我无法理解该错误。

I have 64 bit wamp version 2.4/Apache Version :2.4.4/PHP Version :5.4.16/ 64 bit windows 7 Also i have installed Microsoft Access Database Engine 2010 Redistributable from official site. 我有64位Wamp版本2.4 / Apache版本:2.4.4 / PHP版本:5.4.16 / 64位Windows 7我还从官方站点安装了Microsoft Access Database Engine 2010 Redistributable。

Here is my php code 这是我的PHP代码

<?php
echo (8 * PHP_INT_SIZE) . "-bit<br/>";
$user = "";
$password = "";
$mdbFilename= "C:\wamp\www\test\testdb.accdb";

$conn=$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=$mdbFilename", $user, $password);              ////<----line 10

if (!$conn) {
  exit("Connection Failed: " . $conn);
}

$sql="SELECT * FROM testdb";
$rs=odbc_exec($conn,$sql);
if (!$rs) {
  exit("Error in SQL");
}

while (odbc_fetch_row($rs))
{
    $json_output[] = odbc_result($rs, "test");
    print(json_encode($json_output));

}
odbc_close($conn);
?>  

Here is my error 这是我的错误

Warning: odbc_connect(): SQL error: [Microsoft][ODBC Microsoft Access Driver] Not a valid file name., SQL state S1000 in SQLConnect in C:\wamp\www\test\working.php on line 10

This is what I'm using to connect with a Ms-Access: 这是我用来连接Ms-Access的内容:

<?php
    $conn = new COM("ADODB.Connection");
    $dns = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=".realpath("./youraccessdb.mdb").";";
    $conn->open($dns);
    $rs = $conn->execute("SELECT * FROM table");
    $field =  $rs->Fields(0);

    while (!$rs->EOF) {
      print $f1->value . "\n";
      $rs->MoveNext();
    }
    $rs->Close();
    $conn->Close();
?>

Hope this help. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM