How to do PHP to MS SQL connection? - php

I'm using xampp and my php version is php5.5.6 in WIN-7 os. I want to connect the MS SQL Server from my PC. I'm having MSSQL SERVER in my LAN. how to do that, I'm new to PHP?

Right from the manual - http://www.php.net/manual/en/function.mssql-connect.php
<?php
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
$server = 'KALLESPC\SQLEXPRESS';
// Connect to MSSQL
$link = mssql_connect($server, 'sa', 'phpfi');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
?>
If this code does not work, then make sure you can ping the database server from the web server.

Related

mssql_connect(): Unable to connect to server on windows server 2000

I have application with php. I want to connect on sql server 2000
This is my code:
$server = '10.0.0.26';
// Connect to MSSQL
$link = mssql_connect($server, 'username', 'password');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
But I have error like
Message: mssql_connect(): Unable to connect to server: 10.0.0.8. I
make sure that i use correct username, password and ip.
When I use this code to connect sql server 2012. I did't get any error.
I run my php using apache on linux.
The first parameter should be SERVERNAME not Server's IP as it is mention in the PHP DOCS
eg ( from php manual)
$server = 'KALLESPC\SQLEXPRESS';
// Connect to MSSQL
$link = mssql_connect($server, 'sa', 'phpfi');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
Check your php.ini file:
; Use NT authentication when connecting to the server
mssql.secure_connection = On
If you have secure_connection = On, make sure that you provide valid credentials in the properties for Apache service in the System Services box. Then you should not send DB username and password from your script to MSSQL Server.
If you want to use specific credentials from a PHP script, then set
mssql.secure_connection = Off
in your php script.

Connect PHP on Linux server to MS SQL on remote server running Windows

Im having real trouble getting this to work. I have got the mssql-connect drivers set up and working on the Linux server, it now understands that function and does not return any errors.
I have had our server management team configure the windows server to allow the connection from the linux server. They have tested this and confirm it is all working, however I just cant seem to connect to it with PHP.
I have tried various connection strings, but it won't connect, here is an example
<?php
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
$server = '214.133.182.71,1443';
// Connect to MSSQL
$link = mssql_connect($server, '****', '******');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
?>
I always get:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: 214.133.182.71,1443 in /home/v3rec/public_html/test.php on line 10
Something went wrong while connecting to MSSQL
The username and password of that for the MS SQL database. Ive tried it with and without the port name. I am running SQL 2012, im not sure what the instance name would be? Am I missing something? Why is PHP unable to connect?
May I suggest you use PDO and do this:
$dbhost = "myhost";
$dbport = 10060;
$dbname = "tempdb";
$dbuser = "dbuser";
$dbpass = "password";
try {
$dbh = new PDO ("dlib:host=$dbhost:$dbport;dbname=$dbname","$dbuser","$dbpass");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}

php mysql connect fails even though shell client succeeds

I have a weird problem.
I'm trying to connect from a linux host to a remote MySQL server on a Windows host.
If I try to connect from shell using the mysql client it works perfectly.
#mysql -h 192.168.x.x -u MyUser -pMyPassword
I've also tried connecting from a simple python script and it works as well.
But if I try to connect from a php script it fails with a "Can't connect to MySQL server on '192.168.x.x' (13)" error.
mysql_connect ('192.168.x.x', 'MyUser', 'MyPassword');
the remote server is running 5.0.41-community-nt on Windows (no info about the OS version)
the client machine is running CentOS 6.2 and is itself equipped with a MySQL 5.1.61 server.
The PHP MySQL module is using the sampe API version 5.1.61
The problem MUST be inside PHP as the connection is successful from the shell client (and even from a python script) but I don't have a clue.
Any help will be higly appreciated.
Use the simple PHP way to connect MySQL:
<?php
$link = mysql_connect('192.168.x.x', 'MyUser', 'MyPassword');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
Please, show us the full code.

How to connect to a remote computer running a sql database via php

I am running an apache webserver on a server on my local network.
I have a microsoft SQL server 2008 r2 database running on a different machine on the same network. My webserver is hosting a lot of pages, and are all fine and can be accessed locally and externally. However, when I try to connect to the SQL database using php, I get hit with the following error.:
Failed to connect to MySQL: No connection could be made because the target machine actively refused it.
I am at a total loss at this point as I have opened port 1433 and have spent hours trying to figure this out.
The code that I am using is a very basic connect script:
<?php
// Create connection
$con = mysqli_connect("mycomputer", "myname", "password", "my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
If anyone can help, I would sincerely appreciate it as iIam about to give up on this project!!!
Thank you in advance.
As i understand - you use MS SQL , so you must use mssql_connect not as mysqli_connect
Check here
Cause its another database and another driver (extension ) using to connect

using mssql_connect to connect to a DB in another server

We are using the below script.
Actually our DB is in one server (say www.server1.com) and the PHP file
containing the below connection string is in another server (say
www.server2.com)
If we place the PHP file containing the below script in the same server
where the DB exists that is www.server1.com/dbconnection.php it is working
fine.
But if we place the PHP file containing the below script in another server
www.server2.com/dbconnection.php it is not working. This displays the
error 'Something went wrong while connecting to MSSQL' Please advise.
Also to handle error if we use 'die('MSSQL error: ' .
mssql_get_last_message());' nothing displays just an empty page.
Please advise how to handle errors.
Script (dbconnection.php)
$server = 'xxx.xxx.xxx.x,xxxx'; //IP, Port
// Connect to MSSQL
$link = mssql_connect($server, 'username', 'password');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
// Connect to DB
$db=mssql_select_db("databasename",$link) or die("Unable to select
database ");
Thank You
How to get error message, I do not know, but if you are migrating your PHP app from windows server to linux (FTP servers are more usual on linux), you can try change:
$server = 'xxx.xxx.xxx.x,xxxx'; //IP, Port
to
$server = 'xxx.xxx.xxx.x:xxxx'; //IP: Port
As described in first parameter of manual: http://www.php.net/manual/en/function.mssql-connect.php

Categories