Connect Error to Remote SQL server database from Linux/Red Host

I’m running php 5.5.15 from a RHEL6 server. I’m trying to connect to a remote SQL server but getting the below error.

odbc_connect(): SQL error: [unixODBC][Driver Manager]Data source name not found, and no default driver specified, SQL state IM002 in SQLConnect

I’ve installed php-pdo_odbc added extension=pdo_odbc.so to my php.ini file. I’ve tried to follow some google topics and I’m not sure what I should be doing. Do I need to install unixODBC outside of the php-pdo_odbc driver? Is there other setup needed. There doesn’t seem to be great direction out there for a newb for setting this up. Any suggestions or links would be appreciated.

Trying to connect this way. Not sure if the driver is correct as I’m not sure what it should be set to on RHEL6.

$ods_conn = odbc_connect(“Driver={SQL Server Native Client 10.0};Server=$odsHOST;Database=$odsDBname;”, $odsUN, $odsPW);

if (!$ods_conn){
if (phpversion() < ‘4.0’){
exit(“Connection Failed Php Error : . $php_errormsg” );
}
else{
exit(“Connection Failed ODBC Error:” . odbc_errormsg() );
}
}

debow, are you trying to connect to an ODBC server system or an MS-SQL system?

For a MS SQL system you should use the ms-sql connect function that is in PHP. It is used in this format:
[php]
// connect
$cs = mssql_connect ( ‘server_name:port’, ‘username’, ‘password’ ) or die ( ‘Can not connect to server’ );

// select
mssql_select_db ( ‘[database_name]’, $cs ) or die ( ‘Can not select database’ );

//query
$sql = “SELECT * FROM [TABLENAME]”;
$r = mssql_query ( $sql, $cs ) or die ( ‘Query Error’ );

// loop the result

while ( $row = mssql_fetch_array ( $r ) )
{
/* do stuff */
}
[/php]

Note: ODBC is basically a connection to a local database. If you have one on a local server, you have to set up the ODBC connection info inside of your local computer. Then, you can connect to the ODBC connection that is set up in your system. But, I suspect you are attempting to reach a remote MS-SQL database system because you said “remote SQL”. If you are attempting to connect to a different brand of server, let us know further details…

If you really have an ODBC database on your local machine or LAN, here is a tutorial on how to connect to it using ODBC. (This will NOT work on a server outside your LAN as it is just ODBC and not an external internet connection.)
http://www.w3schools.com/php/php_db_odbc.asp

Sorry for my lack of understanding, I am trying to connect to a remote MS-SQL server. I will try the example you gave and get back to you asap. Thanks for you time.

No problem! So, an ODBC is for local database, like VB, Foxpro or MS-Access where you have the local database set up and your system has been made know of it. Then, you use the code like you showed us
to connect locally.

For remote internet access, the code I gave you should work as long as you have the correct permissions
on the other end.

Let us know…

I added the connection code and get this now, I assume I’ll have to install another driver for mssql_connect to work?

Fatal error: Call to undefined function mssql_connect() in

Well, show us your code… the fatal error should have given you a line number, therefore we would
need to see that line. Perhaps you should post the connection code I gave you in a test file and then
show it to us. If it fails the connection, we would know what line your error is in.
Please place the code inside of the PHP tags. AND, of course, do not give us your username or password.
Never post those!

if you do not have the lastest PHP on your server you might get this type of error. Also, on some servers
you have to alter your php.ini file to allow for connections to be made to the MS-SQL system…

I have php 5.5.15
Fatal error: Call to undefined function mssql_connect() in /FormDev/inc/config.php on line 4

This is what I see in my php.ini related to mssql.

18:03:03 # grep mssql /etc/php.ini
mssql.allow_persistent = On
mssql.max_persistent = -1
mssql.max_links = -1
mssql.min_error_severity = 10
mssql.min_message_severity = 10
mssql.compatability_mode = Off
;mssql.connect_timeout = 5
;mssql.timeout = 60
;mssql.textlimit = 4096
;mssql.textsize = 4096
;mssql.batchsize = 0
;mssql.datetimeconvert = On
mssql.secure_connection = Off
;mssql.max_procs = -1
;mssql.charset = "ISO-8859-1"

config.php
[php]

<?php // connect $cs = mssql_connect ( ':1433', '', '' ) or die ( 'Can not connect to server' ); // select mssql_select_db ( '', $cs ) or die ( 'Can not select database' ); [/php]

Well, I looked around a bit on the net for you. It seems that there are several issues why you get
that error. One, the remote server might not allow external connections. It should, but, who knows.
Next, do you know what extensions you have on your own server. Perhaps it is not set up to allow
remote connections?

There were several comments stating that the hostname or “servername:port” is different for Linux and
for Windows… It states servername:port for one and servername,port for the other. I think the comma
was for the Windows server. My guess is that you are connecting to a Windows server, so try a comma
instead of a semicolon…
SO: ‘:1433’
becomes: ‘,1433’

And, in the error message you posted, there is no line # 4 in your code. It is blank…

I installed the following php55w-mssql and got past the undefined function error. I now get an unable to connect to server:. I’m checking with the remote server owner to make sure they are allowing connections from my server as well as the ports being opened.

Since I use a professional hosting service, I did not think about having to install an addon for the w-mssql…
Sorry, should have thought of that.

Please let us know if your remote server figures it out. Quite often is it the port number or wrong password.
(Two most common issues…)

Let us know…

Will do, thanks for the help.

I’m working with the team that owns the SQL server but was wondering if anyone has seen this error before?

 mssql_connect(): message: Login failed for user '<myUsername>'. (severity 14)

They suggest the username/password is correct. I’ve not found anything concrete about severity 14 yet.

I noticed I now have this after installing php55w-msql withing phpinfo(). Do I need to do something with FreeTDS?

mssql
MSSQL Support	enabled
Active Persistent Links 	0
Active Links 	0
Library version 	FreeTDS 

After working with the owner of the SQL server and getting the correct db name and correct priv’s for the account they gave me I’m now able to connect to the server successfully. Thanks for the help everyone!

Great, glad you solved it. Isn’t programming fun? I love a good programming puzzle…

We will see you in the next puzzle you find for us…

(I will mark this one solved!)

Sponsor our Newsletter | Privacy Policy | Terms of Service