Want to connect PHP5 to MS SQL SERVER 2005 in linux - php

I want to connect mssql server using php code on linux operating system. i want to use mssql_connect() .please help me. my code is
$server='x.x.x.x\SQLEXPRESS';
$link = mssql_connect($server, 'username', 'password');
if (!$link)
{
die("Couldn't connect to SQL Server on $server. Error: " . mssql_get_last_message());
}
else
{
echo "Connected!";
}
it show error "Unable to connect to server: x.x.x.x\SQLEXPRESS"

For me, this worked under debian lenny:
apt-get install libsybdb5 freetds-common php5-sybase
/etc/init.d/apache2 restart
I found the solution here: http://docs.moodle.org/20/en/Installing_MSSQL_for_PHP

Related

Howto connect with PHP to a MS SQL Server within a Docker setup?

I am trying to connect to a MS SQL server with PHP in a Docker environment.
I followed this instructions for my Dockerfile:
FROM php:7.3.10-apache
RUN apt-get -y install unixodbc-dev
RUN pecl install sqlsrv pdo_sqlsrv
The following two lines are added to the php.ini:
extension=pdo_sqlsrv.so
extension=sqlsrv.so
The phpinfo() shows this:
Now I am trying to connect to the MS SQL Server (which is not part of the docker environment):
$server = 'MyServer';
$database = 'MyDB';
$username = 'MyUser';
$password = 'MyPassword';
# Connect
try {
$conn = new PDO("sqlsrv:server=$server;Database=$database", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
die("Error connecting to SQL Server: ".$e->getMessage());
}
But I am getting the following error:
Error connecting to SQL Server: SQLSTATE[IMSSP]: This extension
requires the Microsoft ODBC Driver for SQL Server to communicate with
SQL Server. Access the following URL to download the ODBC Driver for
SQL Server for x64: https://go.microsoft.com/fwlink/?LinkId=163712
Can someone point me in the right direction? What exactly do I need to do?

PHP PDO + PostgreSQL Connection Error: Could not find Driver

There are a few similar questions that I have read through and followed the advice but to no end, and only being fairly new to this, I figured now was a good time to stop 'trying' things in case I break anything any further.
I'm receiving the following error when trying to connect to my database via PDO:
Connection error: could not find driver
1. I've ensured that Apache is linked to my homebrew PHP.
$ which php
/usr/local/bin/php
2. Have uncommented the following in php.ini
extension=php_pdo_pgsql.dll
The pdo_pgsql module shows up in php_info
3. My database connection is:
<?php
class Database
{
public $conn;
public function getConnection()
{
try {
$this->conn = new PDO("postgres://$user:$pass#$host:$port/$db_name");
print_r($this->conn);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->conn->exec("set names utf8");
} catch (PDOException $exception) {
echo "Connection error: " . $exception->getMessage();
exit();
}
return $this->conn;
}
}
I've triple checked these details and they are correct (albeit ommitted). I can connect with the same URI via IntelliJ's Database connection Wizard
4. I’ve edited /usr/local/var/postgres/postgresql.conf to include:
#listen_addresses = '*'
I'm still not having any luck and am looking at some guidance in this head scratcher.
As I see you are using Linux, but tried enable .dll library which is used for Windows machines. It makes sense to comment this line.
Make sure that you have pdo_pgsql module enabled:
# php -m | grep pdo_pgsql
pdo_pgsql
Install it if it is not
# yum install php-pgsql
Here is steps that I did to make PHP+PostgreSQL work on my clean CentOS 7 install:
Install PostgreSQL (but I think you already have this installed and
configured)
# yum install postgresql-server postgresql-contrib
Updated config /var/lib/pgsql/data/pg_hba.conf, changed from ident to md5
host all all 127.0.0.1/32 ident
host all all ::1/128 ident
After
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
Restart postgresql service
# service postgresql restart
Install PHP and PDO connector
# yum install php php-pgsql
Here is an example of PHP script I used to test connection:
<?php
// Configure DB Parameters
$host = "localhost";
$dbname = "masterdb";
$dbuser = "automation";
$userpass = "fGmK4hvDZPB6fr6c";
$dsn = "pgsql:host=$host;port=5432;dbname=$dbname;user=$dbuser;password=$userpass";
try{
// create a PostgreSQL database connection
$conn = new PDO($dsn);
// display a message if connected to the PostgreSQL successfully
if($conn){
echo "Connected to the $dbname database successfully!";
echo "\n";
}
}catch (PDOException $e){
// report error message
echo $e->getMessage();
}
And the output:
# php pdo_test.php
Connected to the masterdb database successfully!

PHP timeout when connecting to mssql under apache

PHP Version 5.4,
Apache Version 2.4,
CentOS 7.3
Trying to connect to mssql db using the following php code which works fine when I run it from the command line. However, when I place code under apache (on same server) and call through browser I get a timeout error.
Error: Connection failed: SQLSTATE[HYT00] SQLConnect: 0
[unixODBC][Microsoft][ODBC Driver 13 for SQL Server]Login timeout
expired+
set_time_limit(30);
echo "+ Connection\n";
try {
$pdo = new PDO("odbc:sqlsrv_msodbc", "username", "password");
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
I just had the same issue. I am using CentOS 7.3 in vagrant and solved it by disabling SELinux.
If you're on a dev environment and disabling SELinux is cool you can do it by:
sudo vi /etc/sysconfig/selinux
Set
SELINUX=disabled
I found this over at serverfault: https://serverfault.com/questions/240015/how-do-i-allow-mysql-connections-through-selinux

Which .so files can be used for linux system corresponding to .dll files in windows to connect php to ms sql server

I have used php5ts.dll files for php and ms sql server connection on my windows sytem but i need to do same for linux but same dll files do not work on linux as they are for windows. After Rnd I came to know that corresponding to dll files in windows we need .so files in linux SO question.
I don't have knowledge that what excatly these .dll files do in php to connect it to ms sql server.
It would be great if some one help me understanding that why same does not work for linux and what could be the solution?
I also came through one same question here where user has asked similar question for windows instead of linux.
Following is the piece of code we use to create connection:
db.driver = "pdo_sqlsrv"
db.host = ""
db.dbname = ""
db.user = ""
db.password = ""
and driver files are placed here at D:\wamp\bin\php\php5.4.16 >>php5ts.dll
and
D:\wamp\bin\php\php5.4.16\ext >> php_sqlsrv_54_ts.dll
To connect from php5 on a linux machine to Micrsoft SQL is a little couter intuitive, you actually need to use the sybase extension and the pear MDB2 driver for MS SQL. The MSSQL functions in PHP5 actually alias the Sybase functions if you can't install the MSSQL extension due to OS limitations.
To solve this use the following commands...
sudo apt-get install php5-sybase
pear install --nodeps MDB2_Driver_mssql
After doing that you can test it using...
<?php
$server = 'servername';
$username = 'sa';
$password = 'password';
$database = 'xxx';
$connection = mssql_connect($server, $username, $password);
if($connection != FALSE)
{
echo "Connected to the database server OK<br />";
}
else
{
die("Couldn't connect");
}
if(mssql_select_db($database, $connection))
{
echo "Selected $database ok<br />";
}
else
{
die('Failed to select DB');
}
$query_result = mssql_query('SELECT ##VERSION');
$row = mssql_fetch_array($query_result);
if($row != FALSE)
{
echo "Version is {$row[0]}<br />";
}
mssql_free_result($query_result);
mssql_close($connection);

MSSQL VIA FreeTDS, ODBC, and Cpanel Unknown host machine name (severity 2)

I have installed FreeTDS 0.91, ODBC, on a Cpanel server running Centos 6.5x64. Everything appears to be running fine and I can connect to the remote MSSQL 2012 server using:
/usr/local/freetds/bin/tsql -S sqlserver -U test -P mypassword
and succesfully execute queries in the database.
I can also connect through:
isql -v sqlserverdatasource test mypasswordhere
But for some reason /usr/local/freetds/bin/tsql -LH server.ip.here
returns no information or errors which doesn't make much sense when it is proven I can connect with the other methods above.
So now when running a test script from a cpanel account on the machine I get:
Unknown host machine name (severity 2)
Here is the test script:
//Database connection function.
function getConnection() {
try {
//$dbconnect = new PDO("sqlserver:Server=server.ip.here,1433;Database=dbname", "user", "password");
$dbconnect = new PDO("dblib:host=server.ip.here,1433;dbname=dbname", 'user', 'password');
} catch (PDOException $e) {
echo "CONNECTION ERROR.<br>Error message:<br><br>" . $e->getMessage();
die();
}
if (!$dbconnect) {
die('Cant connect to database. Please try again later!');
}
else{
echo "i'm in!";
return $dbconnect;
}
}
The first commented line is the old one using sqlserv which I found did not work at all from what i can tell because of the x64 OS. I have also tried with "" around user and pass as well as no marks at all.
php -m does show PDO and pdo-dblib.
Any ideas where I can look next?
Update: This was fixed. I missed in freetds.conf:
[global]
# TDS protocol version
tds version = 8.0
It was originally set to 4.5 instead of 8.
The fix for me was with three steps:
First, I edited /etc/freetds/freetds.conf and changed the tds version like this:
tds version = 8.0
The second step was not entering port number. The port was already 1433, and not specifying it fixed the exact same issue on my case.
Lastly, to connect properly, I had to restart networking as #user1054844 mentioned as this:
/etc/init.d/networking restart
After all these steps, I was able to connect and work with the SQL Server database.
You actually did not need ODBC at all since your connect script is using pdo_dblib not odbc. You can just install FreeTDS than enable pdo_dblib via the compile time flag in rawopts and rebuild via EasyApache. Of course cPanel specifics for this are a bit different.
I just did this for a friend and decided to document it since it is hard to find accurate clear information for FreeTds and pdo_dblib on cPanel.
Guide is here: FreeTDS And pDO_dblib On cPanel

Categories