$dbName = "C:\path\folder";
$user=""; $password="";
if(!file_exists($dbName)) {
die("Could not find database file.");
} echo"connecting...";
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)};
DBQ=$dbName; $user, $password");
the above is my connection string code for .mdb, I am running IIS7 on win7. My php page runs perfectly, but when I include or put the connection string above my page, it loads only until the
echo"connecting...";
and don't load anything under the string ...
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=$dbName; $user, $password");
So I transferred the $db = new PDO()... in the footer area and everything loads above this string.
Can anyone help me with this crazy problem?
From the Manual, the connection string should be
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$dbName", $user, $password);
In your case, it should be
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)};
Dbq=$dbName" $user, $password);
Thanks for your help Friend. I found a working code here. Thanks again, 'till next problem. ^_^
Using PHP to query a MDB file, and return JSON
PDO Query Database ODBC
Actually, I have this now.
$conn = odbc_connect('Driver={Microsoft Access Driver (*.mdb)}; DBQ=Northwind.mdb','','');
if($conn){
echo "Database Connected!";
} else echo "Database Disconnected.";
Hope this will helps those who are new to php and MS Access.
Thanks.
Related
I am fairly new to using PHP. I downloaded XAMPP, and installed everything. PHP 5.5.27 is the version. I ran a test php program which was jsut echo "Hello World". It worked fine. I also was able to connect to MYSQL database using PHP.
$link = mysqli_connect("localhost", "u/n", "pass", "databasename";
Problem i am having and need help is with connecting to sql server. How do i do that? I saw an example online and tried it:
$serverName = "servername";
$connectionInfo = array("Database"="name", "UID"=>"U/N", "PWD"=>"pass";>
$conn = sqlsrv_connect($serverName, $connectionInfo);
But everytime i run this it tells me:
Call to undefined function sqlsrv_connect()
Can someone help me understand what is going on?
Consider using PHP's Data Objects (PDO) to connect to SQL Server (in fact you can use it to connect to MySQL or any other database).
Using the MSSQL sqlsrv API (various dlls must be set):
<?php
$server = 'servername';
$database = 'databasename';
$username = 'username';
$password = 'pass';
try {
$conn = new PDO("sqlsrv:Server=$server;Database=$database",
$user, $password);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
?>
Using the ODBC Driver or DSN API (requiring MSSQL ODBC Driver installed which usually ships with database or Windows in general):
<?php
$server = 'servername';
$database = 'databasename';
$username = 'username';
$password = 'pass';
try {
$dbh = new PDO("odbc:Driver={SQL Server};Server=$server;
database=$database",$username,$password);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
?>
I am trying to connect to a mssql server on my mac through pdo_dblib. I have the freetds.conf updated to the host I want to connect. My phpinfo tells me that I have all the driver hooked up and good to go. Below is the code that I wrote to test if I can connect to the server.
<?php
$servername = "IP";
$port = "port";
$username = "username";
$password = "password";
$myDB = "database";
try {
$conn = new PDO("dblib:dbname=$myDB;host=$servername:$port", "$username", "$password");
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
However I get an error:
Connection failed: SQLSTATE[] (null) (severity 0)
I tried using SQL Developer to connect to the mssql server and it worked. I have been trying to solve this problem for a whole day. What might be the problem? Thanks!
Remove the quotes from the variables in the connection string -
$conn = new PDO("dblib:dbname=$myDB;host=$servername:$port", $username, $password);
I found out the answer!
You have to uncomment TDS version protocol in /usr/local/Cellar/freetds/0.91/etc/freetds.conf.
Found the solution in Why won't my server connect to a remote MSSQL server using PHP mssql_connect?
but why do I have to do this...no idea...wish some one could give some explanation.
I am working on a website that runs by MYSQL and Linux Php 5.3 - and i need to work with this as well as a remote MSSQL database.
I read that PDO this is the way to connect to MSSQL.
It though seems there are both a PDO and a more familiar mssql_connect solution.
I have little to no experience with either PDO or mssql_connect.
On the PHP documentation i find:
Mssql_connect - The familiar expression:
<?php
// Create a link to MSSQL
$link = mssql_connect('KALLESPC\SQLEXPRESS', 'sa', 'phpfi');
// Select the database 'php'
mssql_select_db('php', $link);
?>
PDO - Which i haven't tried before - which needs a driver !(?) :
<?php
/* Connect to an ODBC database using driver invocation */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
So what to choose and why ?
Although I have not tried it yet. So I can't say for sure if it works or not.
PHP Manual says use pdo::dblib http://php.net/manual/en/ref.pdo-dblib.php
Microsoft does have it's own set of drivers but you have to be on a windows machine to use them. http://www.microsoft.com/en-us/download/details.aspx?id=20098
MSSQL connection with PDO:
$db_handle = new PDO("sqlsrv:server=$server; Database=$database", $user, $pass);
MySQL connection with PDO:
$db_handle = new PDO("mysql:host=$server;dbname=$database", $user, $pass);
I don't see what your confusion is?
I'm trying to create a simple connection using PDO (on localhost - xampp):
<?php
try{
// Connect
//$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=.\db\icr.accdb;Uid=Admin");
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\xampp\htdocs\bd\db\icr.accdb;Uid=Admin");
/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM Miasta";
foreach ($dbh->query($sql) as $row)
{
print $row['Nazwa'] .' - '. $row['IDWojewództwa'] . '<br />';
}
/*** close the database connection ***/
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
But then I get this error:
SQLSTATE[HY000] SQLDriverConnect: 63 [Microsoft][Driver ODBC Microsoft Access]General error Unable to open registry key Temporary (volatile) Ace DSN for process 0x5f8 Thread 0x1124 DBC 0x3608134 Jet.
any ideas?
Thanks a lot vodich, here is solution:
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\\xampp\\htdocs\\bd\\db\\icr.accdb;Uid=Admin");
How about this
PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};
Dbq=C:\xampp\htdocs\bd\db\icr.accdb;Uid=Admin");
I have been successful in querying a Microsoft Access 2003 database (.mdb file) and now I am trying to do the same for a Microsft Access 2007. I have tried the following:
if($type[1]=='accdb'){
echo 'accdb';
//2007 Microsoft Access
$connection = odbc_connect("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$mdbFilename;Persist Security Info=False;", $username, $password);
}else{
echo 'mdb';
//2000, 2003 Microsoft Access
$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $username, $password);
}
However, for an access 2007 database I get this error:
Warning: odbc_connect() [function.odbc-connect]: SQL error: [Microsoft][ODBC Driver Manager]
Data source name not found and no default driver specified, SQL state IM002in SQLConnect
in E:\export.php on line 38
In case anyone is wondering how to do this. This worked for me.
try{
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=$mdbFilename", $username, $password);
}catch(PDOException $e){
echo $e->getMessage();
}
IKIK This is uber grave digging but..
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=" . $mdbFilename, $username, $password);
This should work.