简体   繁体   English

mssql_connect 不会连接同一个 SQL Server 上的不同数据库

[英]mssql_connect will not connect different DB on same SQL Server

$conn_161 = "192.168.0.161";  //local serwer adress
$user_161 = "ME";
$pass_161 = "what_is_the_password?";
$connect_161 = mssql_connect($conn_161,$user_161,$pass_161);
mssql_select_db ( 'DUKENUKEN3D' , $connect_161 );

//as requested - 1st DB connection and 1st query
$q_duke = "select * from DUKE /*DB1*/";
$r_duke = mssql_query($q_wartownik,$connect_161); //result

$connect_different_db = mssql_connect($conn_161,$user_161,$pass_161);
mssql_select_db ( 'BIGMAN' , $connect_different_db );

//second db and query
$q_bigman = "select * from BIGPEOPLE /*DB2*/";
$r_bigman = mssql_query($q_bigman,$connect_different_db ); //result

Error:错误:

Warning: mssql_query() [function.mssql-query]: message: Invalid object name 'DUKE'.警告:mssql_query() [function.mssql-query]:消息:对象名称“DUKE”无效。

Yes I know mssql_select_db is old, but I need to use it in this project.是的,我知道mssql_select_db很旧,但我需要在这个项目中使用它。 As you see I try to connect to same server but select 2 different DB at the same time, and run queries in the same php page (to connect data from 2 different DB).如您所见,我尝试连接到同一台服务器,但同时选择 2 个不同的 DB,并在同一个 php 页面中运行查询(连接来自 2 个不同 DB 的数据)。 It seems it is not valiable?好像不靠谱? I even tried to run mssql_select_db just before doing the query to second DB, and then changing it back to first DB.我什至尝试在对第二个 DB 进行查询之前运行mssql_select_db ,然后将其改回第一个 DB。

I understand this is limitation of the library (I will run all queries from LAST selected DB).我知道这是库的限制(我将从最后选择的数据库运行所有查询)。

Is there a workaroud?有解决方法吗? Because all I got is to create page inside invisible iframe and there run php page with different db connection - far from good solution.因为我所得到的只是在不可见的 iframe 中创建页面,并使用不同的数据库连接运行 php 页面 - 远不是好的解决方案。

I would expect that this will work the same as it would if you were running this in a SQL environment directly (eg you can try it in SSMS or from the command line).我希望这与直接在 SQL 环境中运行时一样(例如,您可以在 SSMS 或从命令行尝试)。

You can specify the database name when you reference the table in the query: eg在查询中引用表时可以指定数据库名称:例如

select * from db1.dbo.DUKE 

This is standard SQL Server behaviour whenever you want to refer to an object which is outside the context of the current database.每当您想要引用当前数据库上下文之外的对象时,这是标准的 SQL Server 行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM