How to connect PHP Access mdb script?

Asked By 0 points N/A Posted on -
qa-featured

Hi guys and good day Expert community of TechyV!

I have a PHP script that I want to connect to Microsoft Access Database.

I tried it but I am struggling doing it and having a hard time to connect it.

I ask many friends about it, but sadly I none of them can solve my problem.

So Experts and computer geeks, how can solve this PHP access mdb script problem?

Are there ways or steps to do this such thing? Any help would be nice.

Thank you very much.

William Mmorriss

SHARE
Answered By 0 points N/A #170314

How to connect PHP Access mdb script?

qa-featured

Hi William,

There are 3 ways to connect to  Microsoft access database using PHP .

1) Using ODBC (Open database connectivity) Driver.

2)Using ADDOB Object.

3) Using PHP inbuilt functions like mysql_connect ().

Using ODBC (Open database connectivity) Driver:

<?php

// Microsoft Access

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

?>

 odbc_connect ( string $dsn , string $user , string $password [, int $cursor_type ] )

dsn

The database source name for the connection. Alternatively, a DSN-less connection string can be used.

user

The username.

password

The password.

cursor_type

This sets the type of cursor to be used for this connection. This parameter is not normally needed, but can be useful for working around problems with some ODBC drivers.

The following constants are defined for cursortype:

SQL_CUR_USE_IF_NEEDED

SQL_CUR_USE_ODBC

SQL_CUR_USE_DRIVER

Using ADDOB Object.

ADODB is a database abstraction library for PHP. It allows developers to write applications in a fairly consistent way regardless of the underlying database system storing the information. The advantage is that the database system can be changed without re-writing every call to it in the application.

<?php

$db_connection = new COM("ADODB.Connection", NULL, 1251);

$db_connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:DataDirEmployee.mdb;DefaultDir=C:DataDir";

$db_connection->open($db_connstr);

$rs = $db_connection->execute("SELECT EmpNameLocal, EmpPosLocal FROM tbl_Employee WHERE ID='$IDNo'");

$rs_fld0 = $rs->Fields(0);

$rs_fld1 = $rs->Fields(1);

while (!$rs->EOF) {

    $empNameLoc    = $rs_fld0->value;

    $empWPPos    = $rs_fld1->value;

    $rs->MoveNext();

}

$rs->Close();

$db_connection->Close();

?>

Using PHP inbuilt functions like mysql_connect ().

PHP has lot of inbuilt function to work with databases.To connect to any database there is following three steps.

  • Open a connection to MySQL itself using mysql_connect() function.
  • Specify the database we want to open using mysql_select_db() function.
  • Close the connection using mysql_close() function.

<?PHP

$user_name = "root";
$password = "";
$database = "school";
$server = "127.0.0.1";

$db_handle = mysql_connect($server, $user_name, $password);

$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

print "Database Found ";
mysql_close($db_handle);

}
else {

print "Database NOT Found ";

}

?>

Related Questions