How to connect PHP to DBF file using the ODBC method - php

I have an dbf file. I would like to use PHP to read data out of this dbf file using the ODBC connection method.
I am using xampp to test this.
I have set up the ODBC. Here is the ODBC connection.
<?php
$conn=odbc_connect('TestDBF','');
if (!$conn) {
echo "Failed";
}
?>
This is the error I get:
The dbf file is saved on the desktop. How I can use PHP script to use ODBC connection to read data out of this file?*

If I remember right php doesn't handle dbf out of the box and you need to install an extension or find a version that has been compiled with that capability. THis may have changed though as the last time I used it was in the php 5.x days.
Also there are difference versions of dbf, so you need to make sure yours is compatible with whatever extesion/driver you are using.

How to connect PHP to DBF file using the ODBC method?
Try this:
<?php
/* Microsoft SQL Server using the SQL Native Client 10.0 ODBC Driver */
$odbc = odbc_connect("Driver={SQL Server Native Client 10.0};
Server=$server;Database=$database;", $user, $password);
// Microsoft Access
$odbc = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};
Dbq=$mdbFilename", $user, $password);
// Microsoft Excel
$excelFile = realpath('C:/ExcelData.xls');
$excelDir = dirname($excelFile);
$odbc = odbc_connect("Driver={Microsoft Excel Driver (*.xls)};
DriverId=790;Dbq=$excelFile;DefaultDir=$excelDir" , '', '');
?>

Related

How to set up an ODBC for MS Access 2016?

I am fairly new to databases and also my English is very bad so don't mind my silly mistakes. I am using:
MS Access 2016 to create a local database named as tc.accdb
XAMPP to provide local host connectivity
this php code to check whether local connectivity is achieved or not:
<?php
$con = odbc_connect( "tc" , "" , "" );
if($con)
{
echo "Connected";
}
else
{
echo "Failed" ;
}
?>
But I just cannot set up Microsoft Access driver for my database. Under administrator tools >.....> System DSN, there exist Microsoft Access driver for (*.mdb) .But since I'm using Access 2016, my database has (.accdb) extension.
I've also tried saving my database as (database.mdb) to avoid extension confilicts but I've used some Office16-specific features so it can't be saved as a database with (.mdb) extension.
Note
Using (.mdb) connectivity with my (.accdb) database just didn't do the job as when I try to run the check code it gives me following error.
try this
$connStr = 'odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};' .
'Dbq=C:\\Users\\Igor\\Desktop\\example.accdb;';
$dbh = new PDO($connStr);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
good luck

PHP Cannot connect to PDO ODBC Driver

My php cannot find my odbc driver. I've downloaded and re-installed multiple times. Can anyone help me with this error:
QLSTATE[IM002] SQLDriverConnect: 0 [Microsoft][ODBC Driver Manager] Data
source name not found and no default driver specified"
Here is my php code:
$dbName = "C:\Users\David\Documents\SCHOOLNEW\Assignment5-PROG1800\database\as4.mdb";
if (!file_exists($dbName))
{
die("Could not find database file.");
}
try
{
// Connect
$dbh = new PDO("odbc:Driver={Microsoft Access Driver(*.mdb, *.accdb)};Dbq=C:\Users\David\Documents\SCHOOLNEW\Assignment5-PROG1800\database\as4.mdb;Uid=Admin");
// INSERT data
$count = $dbh->exec("INSERT INTO part(vendorNo,description,onHand,onOrder,cost,listPrice) VALUES ('$vendorNo', '$desc', '$onHand', '$onOrder', '$cost', '$listPrice')");
// echo the number of affected rows
echo $count;
// close the database connection
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
I'm running php with apache on xampp. This all on a local machine. My system is 64 bits. I'm not sure if it has something to do with the system and drive types or my syntax or certain drivers I need to install. I just want to insert data from my form into the database on my computer.
Driver={Microsoft Access Driver(*.mdb, *.accdb)}
is not a valid ODBC driver name because it is missing a space. The correct name for the newer "ACE" ODBC driver is
Driver={Microsoft Access Driver (*.mdb, *.accdb)}
However, in this case PHP is running in the 32-bit environment and trying to open an .mdb database so the older "Jet" ODBC driver ...
Driver={Microsoft Access Driver (*.mdb)}
... will work, too.
Can you place the path after escaping the slashes and then try:-
$dbName = "C:\\Users\\David\\Documents\\SCHOOLNEW\\Assignment5-PROG1800\\database\\as4.mdb";
Escape the slashes in all the paths you have provided in the code and then try.

Connecting to MS SQL server from PHP

I am trying to connect to a SQL Server database from PHP but I'm having problems with the connection string. Here is a rundown of what I've tried and the effect it has had:
Running PHP 5.3.13, Apache 2.2.22, Windows 7, and SQL Server 2008 R2
I've installed SQL Server Native Client 11.0
I've dropped using MSSQL driver, using the SQLSRV driver instead.
Loaded these extensions in php.ini: extension=php_pdo_sqlsrv_53_ts.dll, extension=php_sqlsrv_53_ts.dll,
extension=php_pdo.dll.
phpinfo() shows these as active:
Registered PHP Streams php, file, glob, data, http, ftp, zip, compress.zlib, phar, sqlsrv
PDO drivers mysql, odbc, sqlite, sqlsrv
sqlsrv support enabled
Connection code is as follows:
$serverName = "[servername]";
$connectionInfo = array( "Database"=>"[databasename]");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn )
{
echo "Connection established.\n";
}
else
{
echo "Connection could not be established.\n";
die( print_r( sqlsrv_errors(), true));
}
The string is set up to use Windows authentication, which sqlsrv_errors() reports:
[message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user '[networkname]\[machinename]'
I have also tried this using my network id/pwd, resulting in
[message] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user '[username]'
I do know that the database is up and functional, as I can connect and run queries without problem from the SQL Server client, and from sqlcmd. The SQL Client and the command line work when using my network/machine credentials. The command line client fails when I try with username/password. This would indicate that any PHP connection should use the Windows authentication to connect.
Someone in another thread suggested that changing permissions in a registry entry would work, but this did not help my issue. PHP 5.3 not recognizing Native Client to connect to MS SQL
I also had some success creating a system DSN. The connection test reports success:
Microsoft SQL Server Native Client Version 11.00.2100
Running connectivity tests...
Attempting connection
Connection established
Verifying option settings
INFO: A network alias was found for the DSN server. 'Protocol: DBMSSOCN; Address: [servername]' was used to establish the connection.
Disconnecting from server
TESTS COMPLETED SUCCESSFULLY!
I've searched extensively within stackoverflow.com, and the interwebs in general with no luck. Obviously the db connection is alive using other methods, but what is going wrong in my connection via PHP?
I got this solved by creating a new login using sql server authentication. I hope this helps somebody with similar issues.
SQLSRV uses the absence of a UID (uid==NULL || strlen(uid) ==0) to indicate that it should try set the "trusted connection" flag in the connection string, i.e. connect using the credentials implied by the execution context.
You can't pass a domain login and password (e.g. UID='MYDOMAIN\user') explicitly.
I had a weird compiler version issue with Microsoft's driver for PHP, and solved my connection problem with PHP's ODBC driver:
//$pdo = new PDO("sqlsrv:Server=$hostname;Database=$dbname;", $username, $password); // works with proper driver for PHP.
$pdo = new PDO("odbc:Driver={SQL Server};Server=$hostname;Database=$dbname;", $username, $password); // works with proper driver for ODBC and PHP ODBC.

Connect sql server 2005 using PHP

I'm trying to connect SQL Server 2005 using PHP. I searched in Google but am not getting proper solution, it's showing search results about mssql_connect() is not working properly. By seeing this am not getting anything.
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
mysql_select_db($db_database) or die("Unable to connect to database: ".mysql_error());
This will connect mysql database. I tried replace mysql_connect() with mssql_connect(). But its not working. login.php has
$db_hostname = 'localhost';
$db_database = 'urlstore';
$db_username = 'root';
$db_password = 'tiger';
How can I connect SQL database using PHP.
You need to install MS SQL PHP extensions and then you can work with your MS SQL Server the way you are used to.
Here is the information about the extension and how to install it.
You could use PDO if your server configuration supports it. PDO is an abstraction layer that allows you to connect to many different database types using the same object.
<?php
try {
$dbh = new PDO ('mssql:host='.$mssql_server.';dbname='.$mssql_db, $mssql_login, $mssql_pwd);
$dbh->exec("INSERT INTO tablename(column1, column2) VALUES ('stuff', 'here')");
$dbh = null;
}
catch (PDOException $e)
{
echo $e->getMessage();
}
?>
This code can help you determine if PDO is enabled:
<?php
foreach(PDO::getAvailableDrivers() as $driver)
{
echo $driver.'<br />';
}
?>
OR you can run this:
<?php phpInfo(); ?>
and look for this:
PDO
PDO support PDO drivers
enabled dblib, mysql, odbc, pgsql, sqlite, sqlite2
If it isn't enabled for MSSQL, you can uncomment the "extension=php_pdo_mssql.dll" line in php.ini.
If it still doesn't work, you might want to try this:
ntwdblib.dll - The most common issue is that you do not have the
ntwdblib.dll file installed in your PHP directory (where php.exe is,
or sometimes placing it in the ext directory works as well). This
library can be found with your Enterprise Manager dll's or in your SQL
servers system32 folder. It's generally best to take the file from the
server where SQL Server is installed
-quoted from http://www.helpspot.com/helpdesk/index.php?pg=kb.page&id=13

What will be connection string to Access database file with PHP

I installed WAMP, I have access database file in project folder, but don't have installed Access on my computer.
Can I read and update Access file with PHP even I don't have installed Access?
And what will be connection string to Access database file?
I really need help with this.
All you need is the PHP api for ODBC.
Here is the example from the docs itself:
<?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" , '', '');
?>
// Microsoft Access
Open the Administrative Tools icon in your Control Panel.
Double-click on the Data Sources (ODBC) icon inside.
Choose the System DSN tab.
Click on Add in the System DSN tab.
Select the Microsoft Access Driver.
Click Finish.
In the next screen, click Select to locate the database.
Give the database a Data Source Name (DSN).
Click OK.
$dsn='database.accdb';
$username='';
$password='';
$connect=odbc_connect($dsn, $username, $password);
I'v found this link with a tutorial on how to do it. Be careful that things work differently in windows and UNIX environment, but since you are using a WAMP you should have no problems
<?php
$db = $_SERVER["DOCUMENT_ROOT"] ."/AccessDatabase/reg.accdb"; //AccessDatabase is folder in htdocs where the database is store
if (!file_exists($db))
{
die("No database file.");
}
$dbNew = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=$db; Uid=; Pwd=;");
$sql = "select * from reg"; //reg is table name
$rs = $dbNew->query($sql);
while($result = $rs->fetch())
{
echo $result[0].": ".$result[1].": ".$result[2]."<br />";
}
?>
If u got error like pdo ODBC Drivers not installed
just go to php.ini and find extension = pdo_ODBC Driver and remove the comment(;)
after that restart the apache

Categories