2

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";
}
?>
Community
  • 1
  • 1
raj
  • 83
  • 1
  • 6
  • 14

2 Answers2

5

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

IcedAnt
  • 444
  • 3
  • 12
2

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" , '', '');
?>
Nims Patel
  • 1,048
  • 9
  • 19
  • $connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $user, $password); – raj Jan 19 '18 at 11:24
  • I had the same error message. The ODBC driver was not 64 bit. I had to compile my application as an x86 process and it worked. I just wanted to throw that out there for any else who encounters it. – Nims Patel Jan 19 '18 at 11:26