Why can't I connect to a Sql Server with PHP? - php

I wrote code as below:
<?php
$conn = mssql_connect("xx.xx.xx.xx", "username", "password")
or die("Can't connect to server ".mssql_error());
echo "Success";
mssql_close($conn);
?>
The error is:
PHP Warning: mssql_connect()
[function.mssql-connect]: Unable to
connect to server: xx.xx.xx.xx in
xxxxxxx\conn.php
on line 2 PHP Fatal error: Call to
undefined function mssql_error() in
xxxxxxx\conn.php
on line 3
The server is Apache2.2, the version of Sql Server is 2005, the version of PHP is 5.2.5, and I remove the ; before extension=php_mssql.dll and there is a php_mssql.dll in the ext directory

Could you check your apache log if there is a problem with the mssql extension when the service starts?

Use mssql_get_last_message()
<?php
$conn = mssql_connect("xx.xx.xx.xx", "username", "password")
or die("Can't connect to server ".mssql_get_last_message());
echo "Success";
mssql_close($conn);
?>

Related

Unable to connect to Server: WIN-71KGFN2PPCC\SQLEXPRESS in C:\inetpub\wwwroot\index.php on line 7 Something went wrong while connecting to MSSQL

I have tested my MS SQL Server connection with this PHP code:
<?php
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
$server = 'WIN-71KGFN2PPCC\SQLEXPRESS';
// Connect to MSSQL
$link = mssql_connect($server, 'sa', 'test1234');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}else{
}
?>
and had this warning and error message:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: WIN-71KGFN2PPCC\SQLEXPRESS in C:\inetpub\wwwroot\index.php on line 7
Something went wrong while connecting to MSSQL
I tried many solutions, I also enabled the TCP/IP protocol of my MS SQL Server from the SQL Configuration Manager, adding the port 1433 to it and to the firewall. Any other solution to it? Please help me.

PHP mssql_connect issue

I'm running this php file:
<?php
$link = mssql_connect('db1.peanut.butter.com:3184', 'user', 'pass');
?>
and getting this error:
Warning: mssql_connect(): Unable to connect to server: db1.peanut.butter.com:3184 in /tmp/query.php on line 3
The server is up and running, and I was able to connect and query it from the SSMS.
How should I write the server in the mssql_connect command?
Full name? Just 'db1'? No port?
Thanks!
This function was removed from PHP 7.0.0
Check if that might be the case.

Connection issue with mysqli

$db= mysqli_connect('localhost', "root", "root");
if(!$db) die("Error connecting to MySQL database.");
mysqli_select_db("onlineform", $db);
As I try to connect to the database like shown above, I am getting the following error:
Warning: mysqli_connect(): [2002] No such file or directory (trying to connect
via unix:///var/mysql/mysql.sock) in - on line 3 Warning: mysqli_connect():
(HY000/2002): No such file or directory in - on line 3 Error connecting
to MySQL database.
It's my first time using mysqli, I thought it would be a big improvement over mysql. I checked with MAMP if it's enabled and it is.
Any suggestions?
Check if your socket is correct. If not, override the default socket. I guess you have to try something like this in MAMP:
$link = mysql_connect(
':/Applications/MAMP/tmp/mysql/mysql.sock',
'root',
'root'
);

Setting up MySQL issue under Eclipse PHP

I have a snippet of code under the PHP file in Eclipse PHP.
$username = mysql_real_escape_string( $username );
It generates an error Warning:
mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /web_development/php_work/new_test/test.php on line 37
Looks like the Eclipse PHP couldn't connect to the MySQL of XAMPP, so How do I fix that?
I added two lines
#mysql_connect('localhost','root', '') or die('Could not connect to : ' . mysql_error());
#mysql_select_db('test') or die( "-Unable to select database");
but Eclipse still throws me.
mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Can't connect to local MySQL server through socket '/tmp/mysql.sock'
thx.

How connect php and sql server 2008 R2 using wamp server

Already changed file php.ini
And the index.php file contains:
<?php
$server = 'name/SQLEXPRESS';
$link = mssql_connect($server, 'namepc', 'pw');
if (!$link) {
die('Something went wrong');
}
?>
And the error message on the localhost is:
Warning: mssql_connect() [function.mssql-connect]:. (severity 14)
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server:
Something went wrong
Already made it!
Just create a new usser on managment studio and linked it the new accound to your php code.

Categories