Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8116629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:53:30+00:00 2026-06-06T03:53:30+00:00

I use this code to connect to SQL Server 2012, but it does not

  • 0

I use this code to connect to SQL Server 2012, but it does not work

<?php
$objConnect = mssql_connect("localhost","usr","pass"); 

if($objConnect)  
{  
  echo "Database Connected.<br />";  
  echo mssql_error();
}  
else  
{  
  echo "Database Connect Failed.<br />";
}  

mssql_close($objConnect);  
?>  

It always prints Database Connect Failed.

Also how can I connect to my local SQL Server from an external web server?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:53:31+00:00Added an answer on June 6, 2026 at 3:53 am

    If your edition is SQL Server Express, you should probably be using:

    $objConnect = mssql_connect("localhost\SQLEXPRESS","usr","pass");
    

    Or if it is otherwise a named instance, then

    $objConnect = mssql_connect("localhost\InstanceName","usr","pass");
    

    If you need to connect remotely, then obviously you shouldn’t be using localhost since how does the remote web server locate your localhost? You should be using one of the following (assuming the remote web server can see your machine with IP address 192.168.5.22):

    $objConnect = mssql_connect("192.168.5.22\SQLEXPRESS","usr","pass");
    $objConnect = mssql_connect("192.168.5.22\NamedInstance","usr","pass");
    $objConnect = mssql_connect("192.168.5.22","usr","pass");
    

    Of course your firewall must have port 1433 (and possibly 1434) open in order to accept that connection, and there are a variety of other things that can go wrong here as well.

    However, a little debugging 101 suggestion. Instead of:

    if($objConnect)  
    {  
      echo "Database Connected.<br />";  
      echo mssql_error();
    }  
    else  
    {  
      echo "Database Connect Failed.<br />";  
    }  
    

    Why not:

    if($objConnect)  
    {
      echo "Database Connected.<br />";  
    }  
    else  
    {  
      echo "Database Connect Failed.<br />";  
      echo mssql_error();
    }  
    

    Surely you don’t need to write an error to the page when the database connects successfully. And telling us the actual error message you receive may better equip us to point you in the direction of a solution. A generic “Database Connect Failed” message that you wrote is not going to give anyone any clue about what actually went wrong. But I bet mssql_error() might!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use this code to select password field from the sql server 2012 db,
I have this piece of PHP code: <?php $username=$_POST['username']; $password=$_POST['password']; if($username&&$password){ $connect=mysql_connect(localhost,root,) or die(
I use the following code for connecting php with MS SQL : $server =
I am trying to connect the sql server 2008 with servlet.this is my code.
Trying to use this code to connect the AD PrincipalContext context = new PrincipalContext(ContextType.Domain,
I am trying to use SMO (VS 2010, SQL Server 2008) to connect to
We are migrating ASP code that used ADO to connect to SQL Server 2000.
I use NHibernate in my ASP.NET application to connect to an MS SQL Server
I'm using the SQL Server Driver for PHP to connect to an SQL Server
I am trying to connect to an SQL Server 2012 database called Remisiones. What

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.