Linking MS Access and database server without installing any additional drivers - php

on our company-website, we have an html-form, where an e-mail is generated via php for consulting reasons - which works fine. My superiors want me to save the information of the html-form into an MS Access-database.
I couldn't come up with a solution that directly writes from php into MS Access - so I convinced them to go with MySQL via phpMyAdmin - linking the tables via ODBC in Access.
The connection between php and MySQL was easy, and the connection between MS Access and MySQL would've also been:
To do that, an ODBC-MySQL-Provider is required for each Client to open the Database, which the IT-department doesn't want to install.
We also use an MS SQL Server on an Windows Server - I could also connect php to SQL Server - but in this case we need to install the ODBC SQL Server Provider when linking the tables or the SQL Server Native Clients if we go with Ado. - IT guys:
no installation of providers on all clients.
My question is:
Is there any possible solution for accessing MySQL/SQL Server
from MS Access without the installation of ODBC-Providers at all?
I have really high pressure on this and can't find anything that could satisfy user-needs and expectations.
Thanks in advance
Baris
Edit: the working php/MySQL-Code - based on mysqli
$servername = "----------.de";
$username = "-------";
$password = "--------";
$dbName = "---------";
$port = "-------";
$con= mysqli_connect($servername, $username, $password, $dbName, $port );
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
The following connection-string works for connecting to a server-side ms sqlserver from MS Access with the integrated SQLSRV32.dll-provider. Thanks to GordThompson
cs = "Driver={SQL Server};"
cs = cs & "Server=ip-adress,port-number;"
cs = cs & "Database=database-name;"
con.Open cs, "username", "password"
Since there is a possiblity to connect to the SQL Server without additional provider, I am going for the PDO/SQLServer-combination, and loading the data based on the connection-string above.

Since the overriding concern is to avoid installing additional drivers on the client machines and you have a Microsoft SQL Server available to you then you can
Have your PHP script write the required information to the SQL Server, and then
Create an Access process that retrieves the information from the SQL Server using the "SQL Server" ODBC driver that is installed with every copy of Windows.

First and foremost, PHP can absolutely connect to an MS Access database (specifically the Jet/ACE SQL Engine). In modern day computing, practically all general purpose languages (C#, Java, PHP, Perl, Python, R, VB) can connect to all popular relational database management systems (RDMS) -both file level (SQLite, MS Access) and server level (SQL Server, MySQL, Postgre, Oracle, DB2), each using some type of customized or generalized library.
With PHP's PDO, you can practically interchange the connected backend database. So however you connect to MySQL, you can model such a connection with other databases. The web popularity of PHP/MySQL make it seem they are married to each other but they are not! Below are examples using DSN, Driver, and even PHP's own API, sqlsrv.
MS ACCESS
# USING DSN
$database="C:\Path\To\Database\File.accdb";
try {
$dbh = new PDO("odbc:DSN=MS Access Database;DBq=$database;");
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM tablename";
$STH = $dbh->query($sql);
$STH->setFetchMode(PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
# close the connection
$dbh = null;
# USING DRIVER (NON-DSN)
$database="C:\Path\To\Database\File.accdb";
try {
$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBq=$database;");
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM tablename";
$STH = $dbh->query($sql);
$STH->setFetchMode(PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
# close the connection
$dbh = null;
SQL SERVER
# WITH ODBC DRIVER PDO
try {
$dbh = new PDO("odbc:Driver={SQL Server};Server=$server;database=$database",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM dbo.tablename";
$STH = $dbh->query($sql);
$STH->setFetchMode(PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
# close the connection
$dbh = null;
# WITH SQLSRV PDO
try {
$dbh = new PDO("sqlsrv:server=$server;database=$database",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM dbo.tablename";
$STH = $dbh->query($sql);
$STH->setFetchMode(PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
# close the connection
$dbh = null;
Extra resource for other language connections:
https://github.com/ParfaitG/DATABASE_CONNECTIONS

Related

PHP Connection to SQL Server DB

I am having connection issues when trying to connect from a PHP script to a SQL server database.
I have used the PDO method - My script can be seen below, connecting using Windows authentication, the object referenced in my script has been created in the database, roles and permissions have been defined.
I am testing locally using WAMP, calling a simple file called insert.php from the webroot.
My SQL server is installed on the same machine and my SQL version is 2012.
The script when executed should simply echo the results of the PATIENT table to the screen for now.
In both of my Apache and PHP versions of the php.ini file i have added the necessary extension to use the sqlsrv drivers and installed all the of the necessary drivers.
Yet still I get the error:
could not find driver1
The script:
<?php
try
{
$conn = new PDO("sqlsrv:Server=localhost ;Database=MindTrackDb", "", "");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(Exception $e)
{
die( print_r( $e->getMessage() ));
}
$tsql = "SELECT * FROM PATIENT";
$getResults = $conn->prepare( $tsql);
$getResults->execute();
$results - $getResults->fetchAll(PDO::FETCH_BOTH);
foreach($results as $row) {
echo $row['PATIENT_ID'].''.$row['FORENAMES'].''.$row['SURNAME'].''.$row['EMAIL'];
echo '<br>';
}
?>
My question is 2 part really:
A) Is this the best method to communicate to a SQL server database with PHP or is there a better method?
B) What could the driver error possibly be - I have followed various suggestions from here and nothing has worked.

How can I connect from a website to a Microsoft SQL Database using PHP

Ive worked with the typical PHPMyAdmin interface until now when it comes to databases.
Now Ive been handed a .bak file and pointed to "Microsoft SQL Server Managment Studio" and told to use that.
My problem is that I dont know how to do the most basic things. For instance when I want to connect to a PHPMyAdmin Database I know that I need to define the host, the user, the password and the database and throw that information into a mysqli_connect() and thats that.
So my question is:
How do I connect to my database that I have in my SQL Server Managment Studio? The same way I did before? And if so where do I find the needed information like host, user, password?
I would install the SQLSRV PDO extension. Then use a similar methodology to access the database
$database = "";
$server = "";
$conn = new PDO( "sqlsrv:server=$server ; Database = $database", DB_USER, DB_PASSWORD);
$sql = "SELECT * FROM Table WHERE MemberID =:MemberID";
$iMemberID = 5;
$st = $conn->prepare($sql);
//named placeholders within the execute() as an array.
$st->execute(array(':MemberID'=>$iMemberID));
//OR using bind param..
$st->bindParam(':MemberID', $iMemberID);
while ($row = $st->fetch()){
echo "<pre>";
var_dump($row);
echo "</pre>";
}

How to connect to sql database through php/pdo that has been exported from mysql/phpmyadmin as mydatabase.sql?

I have searched on many different sites and cannot find an answer that specifically answers this question:
I have WAMP Server installed, which installed phpmyadmin as well as MySQL. I can easily connect to the database through php/pdo by using localhost as my hostname.
But my problem is I am trying to connect to a database that I have exported from phpmyadmin as "mydatabase.sql". So now lets say I placed this file on my pc at "C:\Users\Username\Desktop\MyFolder\mydatabase.sql". How would I connect to this database (if this is possible)?
The reason is that I cannot install WAMP Server on all the pcs that would use this program. so would like to be able to connect to the database without having to install any type of servers, etc...
My php at the moment is:
<?php
$dsn='mysql:C:\Users\Username\Desktop\MyFolder\mydatabase.sql';
$username='username';
$password='pass';
try
{
$dbh = new PDO("$dsn",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Connected to Database<br/>';
$sql = "SELECT * FROM users";
foreach ($dbh->query($sql) as $row)
{
echo $row["ID"] ." - ". $row["Name"] ."<br/>";
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
Also, if this is possible, would username and password be necessary to connect to an "off server database"?
If I try:
$dsn='sqlite:C:\Users\Optique\Desktop\optique.sql';
I get:
"Connected to Database
SQLSTATE[HY000]: General error: 26 file is encrypted or is not a database"
But I think that is because SQLite uses a different database format than MySQL (stand to be corrected). So what I need is something similar to the SQLite driver for MySQL.
Thanks in advance!
You cannot find an answer simply because it doesn't exist: it's impossible to connect to an SQL dump through PDO, and it makes no sense anyway: a dump is not a database but simply a collection of INSERT queries. Unlike sqlite, for mysql you need a server to connect with.
To be able to connect, you should import that dump into mysql database first and then connect to that database with PDO.

Trying to connect with sql server database

I'm trying to connect sql server with php, i'm trying to get info from the database..
Now, here is what i got:
try {
$user = '';
$pass = '';
$objDb = new PDO('mysql:host=192.168.10.250;dbname=WEB_POROSIA',
'$user', '$pass');
$objDb->exec('SET CHARACTER SET utf8');
$sql = "SELECT *
FROM 'WEB_POROSIA'
";
$statement = $objDb->query($sql);
$list = $statement->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo $e->getMessage();
}
I receive this error:
SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
This is my first attempt to connect php with sql server, and i don't know what this output means, i mean i don't know what might cause it!
I'm using xampp.
Thanks
It appears that you are trying to connect using the wrong driver.
For Sql Server you might want to use PDO ODBC driver not the mysql method you are trying to use.
You need to use a different DBO driver.
Check out other ODBC drivers, i.e PDO.
The code you have written tries to connect to a MySQL database rather than a MSSQL one.
You'll want to do something like this:
$db_connection = new PDO("dblib:dbname=$db_name;host=$host", $username, $password);
You can find more information here: http://grover.open2space.com/content/use-php-and-pdo-connect-ms-sql-server

How to retrieve data for a webpage from an MS Access database with password

I have an MS Access database file that I want to copy into a MySQL to serve up on a webpage, the problem is the database is passworded. What I want to do is upload the file to the server then either strip the password or open it using the password so I can then copy it across to MySQL.
The password is known and cannot be removed at source.
I would like to do this with PHP if possible.
This is a recurring event, at max twice a day.
Having contacted my hosting the only way to use odbc is to upgrade to dedicated hosting at 10x the price of my current hosting. Looks like this one is a no go unless I can get at the data another way.
To open it, the password should be passed along in the connection string... For PHP using odbc_connect, the syntax is available here. Since you say the password is known, this should work.
To remove it completely, you'd want to just open it in Access and save a copy without the password. I'm not sure that this can be automated easily. If you need to access the data and transfer it repeatedly, I'd say stick with the password int he connection string.
Example from the article linked to:
<?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" , '', '');
?>
Here is the DSN - Less connection code sample :
<?php
$db_connection = new COM("ADODB.Connection");
$db_connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("../databases/database.mdb") ." ;DefaultDir=". realpath("../databases");
$db_connection->open($db_connstr);
$rs = $db_connection->execute("SELECT * FROM Table");
$rs_fld0 = $rs->Fields(0);
$rs_fld1 = $rs->Fields(1);
while (!$rs->EOF) {
print "$rs_fld0->value $rs_fld1->value\n";
$rs->MoveNext(); /* updates fields! */
}
$rs->Close();
$db_connection->Close();
?>

Categories