Home » Php » Connecting PHP to SQL Server – PHP

Connecting PHP to SQL Server – PHP

Posted by: admin February 23, 2020 Leave a comment

Q(Question):

Hi

I want to connect PHP to SQL Server. I use the following code [php]<?
$connection = mssql_connect($host,$username,$password) or die("Couldn’t connect to SQL Server on");
?>[/php]but nothing is displayed. I don’t know what are the requirements for connecting to sql server

looking for ur help.
Thank’s

A(Answer):

Greetings and welcome.

Nothing is displayed because you have not asked it to display anything. If all the information you have provided it is correct ($host, $username, $password) it should connect. How to test if it has is adding one line of code:
(I havce changed this to mysql instead of your mssql, so provided the syntax is the same, just change that (I have never used mssql)
[PHP]<?php
$connection = mysql_connect($host,$username,$password) or die(‘I cannot connect to MySQL because: ‘ . mysql_error());
echo(‘I have successfully connected’);
?>[/PHP]

What I have done:
1. Don’t be lazy – Write out <?php instead of just <? for good practice and in future versions of php that will not work I believe.
2. In the die() function I have written your message (I cannot connect to MySQL because:) in a string and then combined it with the mysql_error() function which displays the error that it has run into. Mssql might have a different function, I am not sure.
3. Made an echo statement which will mean something is displayed on the screen if it ran successfully. The die() function will stop the code there if there is a problem and not display the echo line, but only the die() error.

Hope that helps, but I recommend looking at some tutes if you are still confused.

A(Answer):

Hi

I want to connect PHP to SQL Server. I use the following code
<?
$connection = mssql_connect($host,$username,$password) or die("Couldn’t connect to SQL Server on");
?>

but nothing is displayed. I don’t know what are the requirements for connecting to sql server

looking for ur help.
Thank’s

The very fact that nothing is written to the browser shows you have successfully connected to your database.

Congratulations 🙂

A(Answer):

Thanks for replying. Sorry for writing incomplete code. I use[php]<?
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
$host="LOCALHOST";
$database="testgenerator";
//$connection = mysql_connect($host,$username,$password);
$connection = mssql_connect(‘HAMAYUN\\SQLServer’,’sa’,’hama’) or die("SQL Server Error");
echo "hello";
?>[/php] This display nothing the time when i post on the form.
By searching the google I find that I have to uncomment the line

extension=php_mssql.dll in php.ini.
Now die("SQL Server Error"); is executed and i see the following output on page
SQL Server Error. It means I cannot connect to sql server.
Also I used the following[php]<?
$connection = mssql_connect(‘HAMAYUN\SQLServer’,’sa’,’hama’) or die("SQL Server Error");
$connection = mssql_connect(‘Localhost\SQLServer’,’sa’,’hama’) or die("SQL Server Error");
$connection = mssql_connect(‘Localhost’,’sa’,’hama’) or die("SQL Server Error");
$connection = mssql_connect(‘.\SQLServer’,’sa’,’hama’) or die("SQL Server Error");
$connection = mssql_connect(‘My IP’,’sa’,’hama’) or die("SQL Server Error");
?>[/php] as mension in some helps but no succes.
Sql server management studio use the following to connect to sql server
"Hamayun\SQLServer","sa","hama"
Now how to connect to sql server 2005.
Looking for quick Help
Thanks

A(Answer):

Thanks for replying.

Sorry for writing incomplete code. I use

<?
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
$host="LOCALHOST";
$database="testgenerator";
//$connection = mysql_connect($host,$username,$password);
$connection = mssql_connect(‘HAMAYUN\\SQLServer’,’sa’,’hama’) or die("SQL Server Error");

echo "hello";
?>

This display nothing the time when i post on the form.
By searching the google I find that I have to uncomment the line

extension=php_mssql.dll in php.ini.
Now die("SQL Server Error"); is executed and i see the following output on page

SQL Server Error. It means I cannot connect to sql server.

Also I used the following

<?
$connection = mssql_connect(‘HAMAYUN\SQLServer’,’sa’,’hama’) or die("SQL Server Error");
$connection = mssql_connect(‘Localhost\SQLServer’,’sa’,’hama’) or die("SQL Server Error");
$connection = mssql_connect(‘Localhost’,’sa’,’hama’) or die("SQL Server Error");
$connection = mssql_connect(‘.\SQLServer’,’sa’,’hama’) or die("SQL Server Error");
$connection = mssql_connect(‘My IP’,’sa’,’hama’) or die("SQL Server Error");
?>
as mension in some helps but no succes.
Sql server management studio use the following to connect to sql server
"Hamayun\SQLServer","sa","hama"

Now how to connect to sql server 2005.

Looking for quick Help
Thanks

You’re not using any statements to check the connection:
[php] $connection = mssql_connect(CONNECTION_DETAILS) or die(mssql_error);
if($connection)
{
echo "Connected";
}
else
{
echo "Problem with connection";
}
[/php] Notice the mssql_error – using this will give you exact error info.

A(Answer):

warning:

Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members’ questions.

Please use [code] tags in future.

MODERATOR

A(Answer):

I get the out put "SQL Server Error" which is the die() statement of the line 6 of code[php]<?
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
$host="LOCALHOST";
$database="testgenerator";
//$connection = mysql_connect($host,$username,$password);
$connection = mssql_connect(‘HAMAYUN\\SQLServer’,’sa’,’hama’) or die("SQL Server Error");
echo "hello";
?>[/php]this means that there is some problem while connecting the server.

A(Answer):

I get the out put "SQL Server Error" which is the die() statement of the line 6 of code

<?
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
$host="LOCALHOST";
$database="testgenerator";
//$connection = mysql_connect($host,$username,$password);
$connection = mssql_connect(‘HAMAYUN\\SQLServer’,’sa’,’hama’) or die("SQL Server Error");
echo "hello";
?>

this means that there is some problem while connecting the server.

Right.
Take a look at my last post and change the part that says
[php] mssql_error()
[/php] Change it to
[php] mssql_get_last_message()
[/php]

Regards.

A(Answer):

I use this [php]<?
mssql_connect("Hamayun\SQLServer","xxx","xxx") or die("SQL Error:" . mssql_get_last_message());
?>[/php] the only output i get
SQL Error:

A(Answer):

Hamayun Khan this is the last warning. You must use code tags when you display any code! Failing to comply with the Posting guidelines will result in a ban from this site.

moderator

A(Answer):

Hamayun Khan this is the last warning. You must use code tags when you display any code! Failing to comply with the Posting guidelines will result in a ban from this site.

moderator

Sorry for not using the right code tags.

I m using <? ?> for php <% %> for asp. I don’t know what r the right code tags.

Would you please give me one example so that i will use in future.

Thanks

A(Answer):

Hamayun Khan this is the last warning. You must use code tags when you display any code! Failing to comply with the Posting guidelines will result in a ban from this site.

moderator

Is this is right.

[php]<?
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
$host="LOCALHOST";
$database="testgenerator";
//$connection = mysql_connect($host,$username,$password);
$connection = mssql_connect(‘HAMAYUN\\SQLServer’,’sa’,’hama’) or die("SQL Server Error");
echo "hello";
?>[/php]

A(Answer):

Hamayun Khan this is the last warning. You must use code tags when you display any code! Failing to comply with the Posting guidelines will result in a ban from this site.

moderator

[php] <?
mssql_connect("Hamayun\\SQLServer","xxx","xxx") or die("Error")
?>[/php]

OK I find the way
Thank’s