1

我需要与远程 SQL Server 2000 数据库通信。我在本地使用 MAMP,我想继续使用它。但是,我不知道我需要做些什么来添加从 PHP 与该数据库对话的支持。看起来 PHP 中的 ODBC 或 SQL Server 函数都可以工作,但默认情况下不会安装这些模块。

有人可以提供有关如何在 MAMP 中添加对 ODBC 或 SQL Server 的支持的说明吗?

4

2 回答 2

4

我能够通过以下方式得到他的工作:

  1. 使用Liip 的单行 PHP Apache Module Installer
  2. 配置 freetds.conf 文件
  3. 编写一些 PHP 连接到 mssql 数据库

概括:

  1. 将其粘贴到您的终端中:

    curl -s http://php-osx.liip.ch/install.sh | bash -

    (适用于 OS 10.7)

  2. 在文本编辑器中打开/usr/local/php5/etc/freetds.conf并在最后为您的 mssql 服务器添加一个条目:

    [MSHOSTNAME]
    host = mshostname.example.com
    port = 1433
    tds version = 8.0
    
  3. 在您的站点文件夹中保存一个 PHP 文件并激活 Web 共享。

    <?php
    
     $myUser = "your_name";
     $myPass = "your_password";
     $myDB = "examples"; 
    
     //connection to the database
     $dbhandle = mssql_connect(MSHOSTNAME, $myUser, $myPass)
       or die("Couldn't connect to SQL Server on $myServer"); 
    
     //select a database to work with
     $selected = mssql_select_db($myDB, $dbhandle)
       or die("Couldn't open database $myDB"); 
    
     //declare the SQL statement that will query the database
     $query = "SELECT id, name, year ";
     $query .= "FROM cars ";
     $query .= "WHERE name='BMW'"; 
    
     //execute the SQL query and return records
     $result = mssql_query($query);
    
     $numRows = mssql_num_rows($result); 
     echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>"; 
    
     //display the results 
     while($row = mssql_fetch_array($result))
     {
       echo "<li>" . $row["id"] . $row["name"] . $row["year"] . "</li>";
     }
     //close the connection
     mssql_close($dbhandle);
     ?>
    
于 2011-10-10T15:44:43.167 回答
2

检查这个问题,看起来您需要为您的 PHP 版本获取驱动程序。

这是另一个链接:Connecting to MS SQL server from PHP using MAMP on OSX

于 2009-09-09T17:20:05.740 回答