问题
I'm trying to execute a (very) simple query on Azure MSSql Server using PHP, but it does not work and prints the follow message:
Warning: mssql_query(): message: Invalid object name 'MyTable'. (severity 16)
I believe that the underlying driver is connecting directly the master database and that's why my objects are not available. So the obvious solution could be mssql_select_db() function, but it raises the follow error message:
Warning: mssql_select_db(): message: USE statement is not supported to switch between databases. Use a new connection to connect to a different Database. (severity 16)
So, any of you guys have ever queried successfuly the MS Azure SqlServer using PHP?
Aditional info: 1 - The connection appears to be OK, with no errors. 2 - I can't qualify/prefix my objects with database.schema, otherwise Azure says:
Warning: mssql_query(): message: Reference to database and/or server name in 'myDatabase.dbo.MyTable' is not supported in this version of SQL Server. (severity 15)
The General config is: - CentOS - PHP 5.3.3 - FreeTDS - Apache 2
/etc/freetds.conf relevant part is like follows:
[global]
#TDS protocol version
; tds version = 4.2
[MyServerAtAzure]
host = mydatabase.database.windows.net
port = 1433
tds version = 8.0
database = MyDatabase
client_charset = UTF-8
The tsql output's:
# tsql -C
Compile-time settings (established with the "configure" script)
Version: freetds v0.91
freetds.conf directory: /etc
MS db-lib source compatibility: yes
Sybase binary compatibility: yes
Thread safety: yes
iconv library: yes
TDS version: 4.2
iODBC: no
unixodbc: yes
SSPI "trusted" logins: no
Kerberos: yes
And finally, the PHP code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
# Older FreeTDS installations need the FREETDSCONF Environment variable
putenv('FREETDSCONF=/etc/freetds.conf');
# Current release of FreeTDS uses the FREETDS environment variable. So we set both to be sure
putenv('FREETDS=/etc/freetds.conf');
$link = mssql_connect('MyServerAtAzure', 'user@mydatabase', 'password');
if ( !$link ) die('<br>Oops! CannotConnect');
//mssql_select_db('MyDatabase', $link); # FAILS because you can't use "USE" statement
$sql = "SELECT * FROM dbo.MyTable";
$rs = mssql_query($sql, $link);
?>
The previous resources that I've already visited are: -
- why-is-my-sql-server-query-failing
- use-statement-is-not-supported-to-switch-between-databases-when-running-query
- PHPFreaks
回答1:
You can't perform cross-database queries and, like the error message says, you also can't change database context using USE
. If you want to query from multiple Azure databases, you need to connect to them independently with different connection strings.
Also, did you try specifying the database explicitly (and not connecting to [...].wondows.net
:
[MyServerAtAzure]
host = mydatabase.database.windows.net
port = 1433
Database = myDatabase
tds version = 8.0
client_charset = UTF-8
And also properly prefixing your table with its schema?
$sql = "SELECT * FROM dbo.MyTable;";
来源:https://stackoverflow.com/questions/18452262/how-to-query-in-a-specific-database-on-azure