PHP and MS Access

How can we include the PHPscript to file MS Access (.mdb)?

I tried to include the following code PHP:

$db_path = $_SERVER['DOCUMENT_ROOT'] . '\WebUpdate\\' . $file_name . '.mdb';
$cfg_dsn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" . $db_path;
$odbcconnect = odbc_connect($cfg_dsn, '', '');

But this failed, and I received the following error message:

 Warning: odbc_connect() [function.odbc-connect]: SQL error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified, SQL state IM002 in SQLConnect in C:\web\WebUpdate\index.php on line 41
0
source share
5 answers

Here is a sample to connect and a simple choice ...

<?php
$db_conn = new COM("ADODB.Connection");
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("./Northwind.mdb").";";
$db_conn->open($connstr);
$rS = $db_conn->execute("SELECT * FROM Employees");
$f1 =  $rS->Fields(0);
$f2 =  $rS->Fields(1);
while (!$rS->EOF)
{
    print $f1->value." ".$f2->value."<br />\n";
    $rS->MoveNext();
}
$rS->Close();
$db_conn->Close();
?> 
+5
source

In the file name I look at "\ WebUpdate" - it looks like you have one backslash at the beginning to two at the end. Perhaps you are missing a backslash at the beginning?

0
source
$db_path = $_SERVER['DOCUMENT_ROOT'] . '\WebUpdate\\' . $file_name . '.mdb';

. '/WebUpdate/'.

0

. ISTR,

: MDB -, db4

    $defdir = str_replace("/", "\\", $_SERVER["DOCUMENT_ROOT"]);
    $dbq    =    $defdir . "\\db4.mdb";
if    (!file_exists($dbq)) { die("Database file $dbq does not exist"); }

    $dsn = "DRIVER=Microsoft Access Driver (*.mdb);UID=admin;UserCommitSync=Yes;Threads=3;SafeTransactions=0;PageTimeout=5;MaxScanRows=8;MaxBufferSize=2048;FIL=MS Access;DriverId=25;DefaultDir=$defdir;DBQ=$dbq";
    $odbc_conn = odbc_connect($dsn,"","")
        or die ("Could not connect to Access database $ dsn");
0
source

I am not sure if this is a violation of best practice or security, but I would like to throw out this sentence:

configure the ODBC connection and enable the database password in the odbc preset settings. let odbc join the DSN name and then save.

in your code, just configure the connection, for example:

try {
  $conn = @odbc_connect("DSNName", "", "", "SQL_CUR_USE_ODBC");
  // un and pw parameters are passed as empty strings since the DSN 
  // has knowledge of the password already.
  // 4th parameter is optional

  $exec = @odbc_exec($conn, $insert) or die ("exec error");
  echo "success!";
}
catch (Exception $e) {
  echo $e->getMessage();
} // end try catch
0
source

Source: https://habr.com/ru/post/1703946/


All Articles