PHP mssql_connect()到Azure数据库 [英] PHP mssql_connect() to Azure Database

查看:36
本文介绍了PHP mssql_connect()到Azure数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试设置与我的站点和mssql数据库的连接.我可以连接到数据库,但是无法执行SQL查询.我的代码是

I try to set connection with my site and mssql database. I can connect to database, but i can't execute SQL queries. My code is

$connection = mssql_connect('jass8l1.database.windows.net', 'username', 'password');
        if (!$connection){
            print_r(mssql_get_last_message());
        }else{

            $res= mssql_query('SELECT * FROM [my_database].[dbo].[table]', $connection);
            print_r(mssql_get_last_message());
            $row = mssql_fetch_array($res);

            echo $row[0];

        }

此代码显示错误

Reference to database and/or server name in 'my_database.dbo.table' is not supported in this version of SQL Server.

但是当我在线执行此查询时,链接MANAGE URL,不会发生此错误.我怎么解决这个问题?也许我需要一些其他的PHP驱动程序?

But when I execute this query online, link MANAGE URL, this error does not occur. How can I solve this problem? Maybe I need some additional driver is for PHP?

推荐答案

错误消息的描述性不能超过实际水平!

The error message cannot be more descriptive than it is!

您完全不能使用4字符号(即[DB_NAME].[SCHEMA].[TABLE_NAME].[COLUMN].在SQL Azure中,您应始终使用3字符号(即[SCHEMA].[表格].[列]).

You simply cannot use the 4-word-notation (i.e. [DB_NAME].[SCHEMA].[TABLE_NAME].[COLUMN]. With SQL Azure you shall always use the 3-word notation (i.e. [SCHEMA].[TABLE].[COLUMN]).

对于SQL Azure而言,还需要在连接中显式设置数据库.您可以在SQL Azure中执行 USE [DB_NAME] .

Something more for SQL Azure is that you have to explicitly set Database in your connection. You can not do USE [DB_NAME] in SQL Azure.

当将SQL Azure与PHP结合使用时,建议您阅读如何到:从PHP连接到Windows Azure SQL数据库.

When using SQL Azure with PHP I recommend that you go through the How to: Connect to Windows Azure SQL Database from PHP.

您必须将连接更改为以下内容:

You have to alter your connection to something like:

  $serverName = "tcp:ProvideServerName.database.windows.net,1433";
   $userName = 'ProvideUserName@ProvideServerName';
   $userPassword = 'ProvidePassword';
   $dbName = "TestDB";
   $table = "tablePHP";

   $connectionInfo = array("Database"=>$dbName, "UID"=>$userName, "PWD"=>$userPassword, "MultipleActiveResultSets"=>true);

   sqlsrv_configure('WarningsReturnAsErrors', 0);
   $conn = sqlsrv_connect( $serverName, $connectionInfo);
   if($conn === false)
   {
     FatalError("Failed to connect...");
   }

此外,强烈建议使用Microsoft的MS SQL驱动程序,而不要使用标准的PHP提供的MSSQL(在操作方法"中也有提及).

Also, it is strongly recommended to use Microsoft's MS SQL driver and not the standard PHP provided MSSQL (also referred in the How To).

这篇关于PHP mssql_connect()到Azure数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆