dpiz9879 2011-01-09 18:34
浏览 66

将现有php界面连接到现有SQL Server DB的问题 - odbc错误

I am having enormous trouble connecting an existing (not programmed by myself) PHP interface to an existing SQL Server DB. I have been accessing a copy of this SQL Server DB for testing purposes on my local machine from my own self-programmed PHP interface using odbc "system DSN" interfaces. But probably, the odbc interface on my local testing environment will be looking different than on the real server, I guess.

I am establishing the odbc connection using code that looks like this:

$db_server ="localhost"; 
$db_name  ="testdb"; 
$db_user    ="testuser";   
$db_pass    ="test";

$conn = odbc_connect($db_name, $db_user, $db_pass) or die ('Can not connect '. $db_name); 

This works fine - I can access the db and everything works as it should.

The PHP interface that I am trying to integrate in my testing environment does the connection this way:

$db_server = "localhost"; # I changed that one from the original server name
$db_user   = "testuser";
$db_pass   = "test";
$db_name   = "testdb";
$connection_id = odbc_connect($dbserver, $dbuser, $dbpass);

So it seems the connection on the server is done without mentioning the $db_name field. Sounds strange to me, but I am only a novice concerning SQL via odbc - normally I only use MySQL.

So when the PHP code tries accessing the db, the following error occurs:

Warning: odbc_connect() [function.odbc-connect]: SQL error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified, SQL state IM002 in SQLConnect in D:\xampp\xampp\htdocs\testdb\global.include on line xxx

xxx is the line containing "$connection_id = odbc_connect($dbserver, $dbuser, $dbpass); "

Any ideas how to get that thing working? I am nearly absolutely sure it's got something to do with the odbc datainterfaces defined on my machine, but I have no idea right now.

Any kind of help is highly appreciated!

Thanks, Sasha

  • 写回答

1条回答 默认 最新

  • doumalu9257 2011-01-09 18:41
    关注

    When you connect via ODBC, you don't actually specify the target details of the database, you specify a data source name. This data source name is looked up in the data source registry, and the connection details are obtained from there. So you need to set $db_server to be the data source name, not the actual host name.

    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教