Connecting to a MS Access database from PHP on Linux - php

My client has been running a Windows server for years but we are now moving to a separate Linux machine for the web app I have created for them. Currently we run PHP on the Windows server on which we are able to connect to an MDB file that is on the same disk. This is a file from an external party, the web app uses MySQL. In the new setup we have a Linux web server (Apache/MySQL/PHP) and a Windows 2016 server which are connected via VPN and we have mounted a share on the Windows server in which the MDB file is located. So far, so good, however I can't seem to query the MDB file. The connection is made, not error there, but every query I run returns an error or nothing not sure. This is my code:
<?php
$db=new PDO("odbc:Driver=MDBTools; DBQ=/mnt/<dir>/<file>.mdb;");
$query=$db->query("SELECT * FROM <table>;");
$return=array();
if($query) {
while($result=$query->fetch(PDO::FETCH_ASSOC)) {
$return[]=$result;
}
}else $return['error']=1;
//close
$query=null;
$db=null;
print_r($return);
?>
Currently everything returns error > 1.
PDO throws the following error:
Connection failed: SQLSTATE[08001]: Client unable to establish connection: 1 Couldn't parse SQL (SQLExecute[1] at /build/php7.2-pRoOsC/php7.2-7.2.24/ext/pdo_odbc/odbc_stmt.c:260)

I found the solution to my problem was removing the ; from the query.

Related

Unable to open database file when the App is installed in Program Files in Windows 7

I have a PHP Desktop application. This app is successfully installed and tested in Windows XP, however, there is a problem when it is installed in Windos 7 under c:\Program Files\myApp folder, the database file couldn't be open and results in the error message:
SQLite3::query: Unable to open database file in C:\Program
Files\myApp\www\checkLogin.php
Note:- My database is in the same
location as the PHP script,i.e, www\myDatabase.db .
The code I used to connect to the database is:-
class MyDB extends SQLite3
{
function __construct()
{
$this->open('myDatabase.db');
}
}
$db = new MyDB();
if(!$db)
{
echo $db->lastErrorMsg();
}
If I reinstall the app in some other location, say, C:\myApp, there is no problem and the app runs successfully.
(Personally I think it is more of a security issue of the Operating System.)
So, what causes the database file to be debarred from opening in the Program Files folder? And what to do to overcome this?
It is usually advised to install directly to your root drive example C:\myApp this is because of windows User Account Control setting prevents some certain functionality to protect your system.
Changing the UAC setting's may allow your program to run from any other directory but exposes your computer to other risks from malicious programs.

PHP Connection to SQL Server 20005

I have PHP installed on a web server and also have MySQL on the same server and all works perfectly.
I now have SQL Server 2005 installed on another server 192.168.90.250 that I want to connect to as there is a view i want to display in PHP.
I have read around for a number of days but still can not get a connection to work.
I am running on the web server:
Windows Server 2008 R2
Apache 2.2
P 5.3.0
And SQL Server 2005 on another server
I presume I need to download a driver like SQLSRV30 and place the correct .dll file into the C:\Program Files (x86)\PHP\ext directory.
I presume I then need to edit the php.ini file and add something like the following:
extension=php_sqlsrv_53_ts.dll
I would then need to restart the apache server.
At this point I presume I would be able to use something like the following to connect to SQL Server:
<?php
if (function_exists('mssql_connect')) {
die("Your PHP installation does not have MSSQL support.");
}
$db = mssql_connect ($hostname, $username, $password);
if($db){
echo "no connection";
}else{
echo "connected";
}
?>
It seems as if I can not run the function mssql_connect, as the page just displays a blank page.
If I comment out the:
//$db = mssql_connect ($hostname, $username, $password);
the page at least displays some data.
I have literally spent days on this, what am I doing wrong?
Am I using the correct driver as there are different versions?
Thanks for your help.
D
For using the sqlsrv extension you need to use sqlsrv_ functions or better yet switch to PDO

PHP Connect to MSSQL From Both Linux and Windows

I am using a linux build server (http://circleci.com) for auto builds and testing; however i need to connect to a MSSQL server. I can develop on windows fine and connect using the MS php SQLSVR drivers, but i cannot connect on the Linux build server as the drivers are windows only.
My Question is this: is there a single way to connect to MSSQL server via php for both windows and linux? tried odbc but then you need bdlib and FreeTDS on linux - this would constitute a code change thus meaning the tests are not 100%, for example on windows environment it would use odbc:{SQL Server} and on linux it would be odbc:FreeTDS
It just doesn't seem right to have a check to see what system is being used and then choose the corresponding db connection string.
I am using PHP 5.5.1
No, there is not a single way however it would be trivial to do something like this:
if ('Linux' === PHP_OS){
$pdo = new PDO("dblib:dbname=$database_name;host=$database_server", $username, $password);
}else{
// $pdo = whatever you're using on your Windows box now.
}
You will need to set up your odbc.ini, odbcinst.ini and freetds.conf files on the Linux server as well.

php can't see local mysql server

I'm just getting started trying to set up a server/website and have run into what is probably a very basic issue. I'm getting a blank screen when I try to connect to my installed mysql server with a php script; e.g.
<html>
<body>
<script src="http://protovis-js.googlecode.com/svn/trunk/protovis-r3.1.js" type="text/javascript"></script>
<?php
echo "Connecting..."
$user="root";
$password="passwd";
$server="127.0.0.1";
mysql_connect($server,$user,$password);
echo "Connected.";
?>
</body></html>
shows "Connecting..." but never reaches "Connected". There's nothing wrong with the code or database, since this did work within the browser in Aptana 2.05 (since uninstalled). It doesn't work within Firefox or Chrome. Aptana was using a different (older) version of PHP, 5.2; phpinfo() accessed from Chrome/Firefox was 5.3. The php.ini file contains extension=mysql.so; otherwise I couldn't tell if the settings were correct or not. The tutorials I looked at didn't really cover this sort of thing... anybody know what to do?
What browser you're using has nothing to do with whether the php script running on the server connects to a database or not.
I would suggest checking the server logs, or adding some code to actually check and see what's going on:
$link = mysql_connect($server,$user,$password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
Your MySQL instance may be configured to not respond to localhost. There are two ways of connecting to a MySQL server; through a network connection (ip and port) or socket connection, which only works on the machine it's working on. Your MySQL server may be configured to listen to socket connections but not network.
Try connecting using the mysql command line tool as "mysql -h 127.0.0.1 -uroot -ppassword mysql"
Try running your PHP script from the command line instead of inside the browser.
Ha! Just didn't have the php5-mysql package installed. So evidently having php and mysql by themselves is not enough. Installed php5-mysql, the connection worked.

PHP MySQL fails when page is viewed remotely

I have two servers on my local network - one a web frontend and the other a MySQL backend. I have a PHP script that looks like this:
<?php
error_reporting(-1);
echo "Connecting...\n";
$link = mysql_connect("192.168.1.15", "-----", "-----") or die(mysql_error());
echo "Communicating with the server...";
mysql_query("INSERT INTO .....
//More code down here...
?>
This script is called on my web frontend to connect to the backend server. When this script is accessed from the local network (i.e. when I open the page by going to http://192.168.1.14), the script outputs
Connecting...
Communicating with the server...
and a row is added to the database, as it should. However when I connect remotely (i.e. going to http://myDomainName.com/mysql_insert_script.php) from a connection not on the local network, all I see is:
Connecting...
No error messages follow, the script just cuts off, and no data is added to the database. When I place a second, 'proxy' script on the server that simply requires() the above script and then I access the proxy remotely, everything works fine. Below is the proxy script, so you can get a better idea of what works and what does not:
<?php
//this script makes it appear that the mysql_script is being viewed from the local network
//I exist on the web frontend at 192.168.1.14
require("http://192.168.1.15/mysql_insert_script.php");
?>
I am sorry if I can't provide any more information, but I am stumped. Any help would be appreciated.
Chris
P.S. - I have verified that the mysql server is accessible from external hosts on the local network, but I have a firewall that prevents connections from outside my network. I don't think this would matter, however, as the MySQL server and the PHP script connecting to it are both run on the local net.
you got wrong server name

Categories