PHP 连接 SQL server 2005/2008 方法 [ 新手入门 ]
下载 SQL ext
http://www.microsoft.com/en-us/download/details.aspx?id=20098
解压到php\ext\中
PHP.ini 加入
extension=php_sqlsrv_54_ts.dll
extension=php_pdo_sqlsrv_54_ts.dll
4.重启Apache, ok.
Btw, I use XAMPP v3.2.1. And below is one of Sample:
<?php
$serverName = "Your Server name";
$connectionInfo = array( "Database"=>"Your database");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
echo "Could not connect database\n";
die( print_r( sqlsrv_errors(), true));
}
$server_info = sqlsrv_server_info( $conn);
if( $server_info )
{
foreach( $server_info as $key => $value)
{
echo $key.": ".$value."\n<BR><BR>";
}
}
else
{
echo "Error in retrieving server info.\n<BR>";
die( print_r( sqlsrv_errors(), true));
}
/* Set up and execute the query. Note that both ReviewerName and
Comments are of SQL Server type nvarchar. */
$tsql = "select *
FROM tbl_status
";
$stmt = sqlsrv_query( $conn, $tsql);
if( $stmt === false )
{
echo "Error in statement preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Retrieve the number of fields. */
$numFields = sqlsrv_num_fields( $stmt );
/* Iterate through each row of the result set. */
while( sqlsrv_fetch( $stmt ))
{
/* Iterate through the fields of each row. */
for($i = 0; $i < $numFields; $i++)
{
echo sqlsrv_get_field($stmt, $i,
SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR))." ";
}
echo "<BR>\n";
}
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
共 0 条回复
没有找到数据。
Scott_Huang
注册时间:2013-07-16
最后登录:2015-04-30
在线时长:5小时6分
最后登录:2015-04-30
在线时长:5小时6分
- 粉丝0
- 金钱65
- 威望0
- 积分115