blackbird
blackbird

Reputation: 39

Using PHP to connect to SQL Server

Using php I am trying to connect to a SQL Server table which has a hyphen in the name, error returned is

Warning: mssql_select_db() [function.mssql-select-db]: message: Could not locate entry in sysdatabases for database 'SMD'. No entry found with that name. Make sure that the name is entered correctly. (severity 16) in C:\xampp\xampp\htdocs\second.php on line 12

Warning: mssql_select_db() [function.mssql-select-db]: Unable to select database: SMD-GROUP in C:\xampp\xampp\htdocs\second.php on line 12
Couldn't open database SMD-GROUP

Code is

    $myDB = "SMD-GROUP"; 
     $selected = mssql_select_db($myDB, $dbhandle)
     or die("Couldn't open database $myDB"); 

Upvotes: 1

Views: 3320

Answers (3)

kingmaple
kingmaple

Reputation: 4310

It is better if you use PDO class for database connections with PHP, since PDO supports multiple DB drivers and is better supported. PHP Data Objects

$DB=new PDO('dblib:host='.$host.';dbname='.$database,$username,$password);

Upvotes: 0

alfred abalos
alfred abalos

Reputation: 51

if you are using MSSQL server 2005 or higher.. you should use SQLSRV drivers by microsoft. Be careful, there are still incompetency on the performance due to unresolve bugs on PHP5.4 and up. You should know if your PHP is compiled on VC6 or VC9 also if it is thread safe or not.. better to see this reference http://php.net/manual/en/book.sqlsrv.php.

remember that php_mssql.dll is already deprecated on PHP5.4 and higher. Also, I already test this on our UAT and Producion servers. Cheers!

Upvotes: 1

prayagupadhyay
prayagupadhyay

Reputation: 31192

Have a look at http://php.net/manual/en/function.mssql-select-db.php:

To escape the name of a database that contains spaces, hyphens ("-"), or any other exceptional characters, the database name must be enclosed in brackets.

Upvotes: 6

Related Questions