2
votes

I have connect Ms Access Database to PHP File. PHP file Give Error

"Warning: 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:\wamp\www\PI\Connection.php on line 3".

Connection.php

<?php
$con = odbc_connect("PIInstitute","","");
if($con){
    echo "Connected";
}else{
    echo "Failed";
}
?>
2

2 Answers

5
votes

You need to specify your driver when calling odbc_connect() like so:

$conn =  odbc_connect ( "Driver={SQL Server};Server=$servername;Database=$dbname;", $username, $password ) or die ( "Connection failed: " . $conn );

You can find more info on odbc_connect()here: http://php.net/manual/en/function.odbc-connect.php

2
votes

The connection id returned by this functions is needed by other ODBC functions. You can have multiple connections open at once as long as they either use different db or different credentials.

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

<?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" , '', '');
?>