Jump to content

[SOLVED] Spaces in Database names (in this case mssql) not connecting


nicktherockstar

Recommended Posts

Hi all,

 

I have been searching the net all week for an answer to this one but still nothing....

 

in short... I have a script which runs connecting up to a bunch of remote (mssql) databases and pulls back a query from them.

 

I am sorry to say however that it will only connect to db's with no spaces in the name. for instance..

 

datalogger001 - connects ok

datalogger 002 - gives error (cannot connect to datalogger) it only seems to use the name up to the first space.

 

With the situation I am in I cannot change the DB names as it is all to do with older and never versions and how they were built at the time, too much uses the db name so they just cannot be changed.

 

Any help on this would make me a very happy man :D

 

yea sorry, it was on another pc so had to mem stick it to get it on here...

 

//connection to the database

$dbhandle = mssql_connect($myServer, $myUser, $myPass)

  or die("Couldn't connect to SQL Server on $myServer");

 

//select a database to work with

$selected = mssql_select_db($myDB, $dbhandle)

  or die("Couldn't open database $myDB");

 

the vars are passed into the function but I have tried it plain text in their place.

 

Imagen $myDB is "datalogger001" it will connect

if its "datalogger 001" it will not.

 

When it reports back the DB name it only returns it up to the space which is very odd. ???

 

 

addition:

 

this is the error( Could not locate entry in sysdatabases for database 'datalogger')

the name it attempted to use however as 'datalogger master'

    The database name.

 

    To escape the name of a database that contains spaces, hyphens ("-"), or any other exceptional characters, the database name must be enclosed in brackets, as is shown in the example, below. This technique must also be applied when selecting a database name that is also a reserved word (such as primary).

 

mssql_select_db('['.$databaseName.']', $connection);

 

AFAIK this is similar to how it works in MS Access. If you want non standard names, use []. I'd strongly advise anyone to use standard names though (standard, as formed with letters of English alphabet, underscores and digits, and not being reserved words)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.