How to avoid connection error in php when ms sql server database name contains special characters

Suppose your database name is my-db-name, you can observe that this database name contains special character hyphen (-), Now if you want to connect with ms sql server in php using this code then you will get error because database name here contains special characters and such special characters need special care :), All you need to do is just put this database name in square brackets []. Now if you assign database name to $db_name like this way $db_name="[my-db-name]"; then you will not get any error and you will be successfully connected with ms sql server database else you will get error if you simply copy/paste the below code and your database name contains special characters.

<?php

$host="localhost"; \\name of the web server

$user="root"; \\username of ms sql server will come here

$pass="yourpassword"; \\password of ms sql server will come here with respect to username

$db_name="my-db-name"; \\name of your database will come here

mssql_connect("$host","$user","$pass") or die("cannot connect to the database server");

mssql_select_db("$db_name") or die("cannot select the database");


?>

so this is the proper way to avoid connection error in php when ms sql server database name contains special characters.

0 comments: