Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
本文是PHP on Windows Azure 入门教学系列第二篇文章。请上一篇文章中,我们已经成功的在Windows Azure中运行了PHP应用。本文会讲解如何以PHP连接至SQL Azure。请读者按顺序阅读本教学系列,因为本文会用到上一篇文章中所创建的项目。
代码:
1.想让PHP代码能够连接到SQL Azure,首先我们必须为PHP加入SQL Server的驱动模块。(SQL Azure 构建于SQL Server技术之上)。从这里下载到SQL Server Driver for PHP 2.0 CTP2 - June 2010。
2. 解压之后,我看到有如下文件。

驱动为phpp_sqlsrv开头的dll,php_pdo开头的dll为其对应的PDO拓展模块。52,53对应的是PHP版本,PHP5.2 和PHP5.3。nts 代表non-thread-safe,ts代表thread-safe。前一篇文章选择的是PHP5.3.2 非线程安全版本,所以我们的SQL Server驱动便是php_sqlsrv_53_nts_vc9.dll。
3. 将php_sqlsrv_53_nts_vc9.dll 拖入PHP_WebCgiRole项目内,php目录下的ext目录。
同时,找到php目录下,php.ini-develop文件,重命名为php.ini。并右击php.ini->Properties。将其Build Action 改为Content。
打开php.ini。搜索extension_dir。将对应处 改为如下:

此举告知程序在ext目录下搜寻拓展模块。
同时将extension = php_sqlsrv_53_nts_vc9.dll 添加到php.ini。如下图;

4. 再将先前的index.php的内容替换如下(参考上一篇文章):
<?php
$serverName = "tcp:ServerID.database.windows.net, 1433";
$connectionOptions = array("Database" => "ExampleDB",
"UID" => "Username@ServerID",
"PWD" => "Password",
);
$conn = sqlsrv_connect($serverName, $connectionOptions);
if($conn === false)
{
die(print_r(sqlsrv_errors(), true));
}
else
{
echo “success”;
}
?>
5.参考SQL Azure 入门教学(一):SQL Azure之初体验该文,将上一步 黄框内容替换为相应信息。
6.按F5 ,调试程序。我们应该能够在浏览器看到success 字样。这表示我们的sql server 驱动已安装成功,并且能够连接至SQL Azure。
7. 接下来我们创建为数据库新建一个数据表。在原有代码下方添加如下代码:
$createTBLsql = "CREATE TABLE [dbo].[VisitCounterTbl](
[ID] [int] IDENTITY(1,1) NOT NULL,
[IPAddress] [varchar](15) NOT NULL,
[VisitTime] [datetime] NOT NULL,
CONSTRAINT [PK_VisitCounterTbl] PRIMARY KEY CLUSTERED
(
[ID] ASC
)
) ";
$createTBLstmt = sqlsrv_query($conn, $createTBLsql);
if($createTBLstmt === false)
{
die(print_r(sqlsrv_errors(), true));
}
else
{
echo "Table successfully created.";
}
执行后 ,应该能在网页看到Table successfully created消息。这样,我们便能在数据库下看到有VisitCounterTbl,它有三个字段,ID,访问者IP(IPAddress),访问时间(VisitTime)。(注意,本文的访问计数器只做演示之用)
8.将index.php代码替换为如下内容 :
<html>
<head>
<Title>Counter</Title>
</head>
<body>
<h1>Welcome! Windows Azure</h1>
<?php
$server = "tcp:ServerID.database.windows.net,1433";
$user = "Username@ServerID ";
$pass = "Password ";
$database = "ExampleDB ";
$connectionoptions = array("Database" => $database,
"UID" => $user,
"PWD" => $pass
);
$conn = sqlsrv_connect($server, $connectionoptions);
if($conn === false)
{
die(print_r(sqlsrv_errors(), true));
}
$insertSql = "INSERT INTO VisitCounterTbl (IPAddress,VisitTime) VALUES (?,GETDATE())";
$ip = $_SERVER['REMOTE_ADDR'];
$stmt = sqlsrv_query($conn, $insertSql, array(&$ip));
if($stmt === false)
{
die(print_r(sqlsrv_errors(), true));
}
else
{
$sql = "SELECT COUNT(ID) FROM VisitCounterTbl";
$stmt3 = sqlsrv_query($conn, $sql);
if($stmt3 === false)
{
die(print_r(sqlsrv_errors(), true));
}
if(sqlsrv_has_rows($stmt3))
{
$row = sqlsrv_fetch_array($stmt3);
echo "<h2>You are the $row[0](th) visitor.Your IPAddress is $ip</h2>";
}
}
?>
</body>
</html>
同样的,将上述代码的 黄框内容替换为相应信息。
当访问者每次访问时,代码每次插入一条记录,并取出所有的记录数量作为网站总访问次数。(注意,本文的访问计数器只做演示之用)
9.我们将项目部署至Windows Azure上。具体部署步骤请参看这里。
之后通过网址访问,能看到如下内容:

本文参考自:https://blogs.msdn.com/b/brian_swan/archive/2010/02/12/getting-started-with-php-and-sql-azure.aspx