Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 Development Tools
 Other Development Tools
 PHP and MSSQL: Is mssql_select_db() required?
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

2 Posts

Posted - 09/23/2014 :  11:01:26  Show Profile  Reply with Quote
In my PHP code, I am calling a stored proc on an MSSQL database (2008 R2). Note: The db admin has set up my db user to use a database by default.

So since my db user has the database defaulted for it, do I have to use the mssql_select_db() in PHP? The code runs fine without the call (as far as I know), but I don't know if perhaps it's required for something that I'm just not seeing the effects of right now. Every single example of connecting to MSSQL on the internet shows the mssql_select_db() call -- but I want to know if it's really necessary in my case.

The reason I want to remove it is to speed up the process of connecting and executing the sp.

My PHP code looks like this:

$sqlLink = mssql_pconnect($serverName, $user, $password);

if ($sqlLink) {
    // needed?
    mssql_select_db($p21DbName, $sqlLink);

    $stored_proc = mssql_init("[dbo].[stored_proc_name_here]", $sqlLink);
    mssql_bind($stored_proc, "@customerID", $cust_num, SQLINT4);
    mssql_bind($stored_proc, "@SKU", $SKU, SQLVARCHAR, false, false, 15);
    mssql_bind($stored_proc, "@qty", strval($quantity), SQLINT4);
    $proc_result = mssql_execute($stored_proc);

Thank you!

Almighty SQL Goddess

38200 Posts

Posted - 09/23/2014 :  12:07:18  Show Profile  Visit tkizer's Homepage  Reply with Quote
You should never rely on the default database being set. Always explicitly connect to the intended database in your connection string.

Tara Kizer
SQL Server MVP since 2007
Go to Top of Page

Starting Member

2 Posts

Posted - 09/23/2014 :  15:23:52  Show Profile  Reply with Quote
Thank you Tara.
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2019 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.04 seconds. Powered By: Snitz Forums 2000