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.

 All Forums
 Development Tools
 Other Development Tools
 PHP and MSSQL: Is mssql_select_db() required?

Author  Topic 

Starting Member

2 Posts

Posted - 2014-09-23 : 11:01:26
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 - 2014-09-23 : 12:07:18
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 - 2014-09-23 : 15:23:52
Thank you Tara.
Go to Top of Page

- Advertisement -