I was recently thrown a real curveball of a project. Instead of using a fresh MySQL database to pull information from, the customer required that we pull information from their new Microsoft SQL Server 2005 server. This isn't the most desired method of PHP->Database interactivity but that's what the project called for and that's what I needed to do. Here's how to get things going.
最近,我被一个项目吸引了。 客户要求我们从其新的Microsoft SQL Server 2005服务器中提取信息,而不是使用新MySQL数据库来提取信息。 这不是PHP-> Database交互性的最理想方法,但这就是项目所需要的,而这正是我需要做的。 这是解决问题的方法。
从PHP连接到Microsoft SQL Server 2005 (Connecting to Microsoft SQL Server 2005 from PHP)
// the "driver" first
$virtual_dsn = 'DRIVER={SQL Server};SERVER=database.domain.org,45277;DATABASE=MyDatabase';
$connection = odbc_connect($virtual_dsn,'Username','Password') or die('ODBC Error:: '.odbc_error().' :: '.odbc_errormsg().' :: '.$virtual_dsn);
选择哪个数据库 (Choose Which Database)
odbc_exec($connection,'USE MYDB');
执行查询 (Executing a Query)
$result = odbc_exec($connection, 'SELECT FirstName FROM Names');
使用查询结果 (Using the Query Result)
$first_name = odbc_result($result, 'FirstName')
查询带有空格的表和列 (Querying Tables and Columns with Spaces)
// use brackets [] for tables and colums with spaces
$result = odbc_exec($connection, 'SELECT [First Name] FROM [Names Table]');
Like I said, while this isn't the most desirable setup in the world, it will do. Big ups to Hostway for their help in configuring the server; they provided everything we needed.
就像我说的那样,虽然这不是世界上最理想的设置,但它会做到。 Hostway非常感谢他们在配置服务器方面的帮助; 他们提供了我们所需的一切。
To learn more about the ODBC functions, visit PHP.net
要了解有关ODBC函数的更多信息,请访问PHP.net
翻译自: https://davidwalsh.name/php-microsoft-sql-server-mssql-iis-connect-query-odbc