首先搭建PHP的使用环境,一般都是使用PHP+mysql+apache的环境,也可以用PHP+mysql+IIS,其实也可以用PHP+mssql+apache的环境,我这里要说的就是PHP+mssql+apache的连接方法。
1、用PHPnow 直接就可以搭建起PHP+mysql+apache的环境,这里就利用这各环境,也可以连接到mssql的数据库的。
2、找到php-apache2handler.ini下面连个地方,去掉前面的分号,就是开起Mssql 数据库。
extension=php_pdo_mssql.dll
extension=php_pdo_mysql.dll
3、将ext文件夹里的文件copy到system32下面。
php_mssql.dll
php_pdo_mssql.dll
4、将php 文件夹了的 ntwdblib.dll文件copy到system32下面。有些有这个文件就不用,但要注意版本。
5、看php环境的版本信息可以使用<?phpphpinfo();?> 来查看。
通过这几步就搭建好了PHP+mssql+apache的环境了。
下面就是连接数据库了。
方法一:
[cc lang=”php”]
<?php
$myServer = “loaclhost”;
$myUser = “guests”;
$myPass = “”;
$myDB = “db”;
//create an instance of the ADO connection object
$conn = new COM (“ADODB.Connection”)
or die(“Cannot start ADO”);
//define connection string, specify database driver
$connStr = “PROVIDER=SQLOLEDB;SERVER=”.$myServer.”;UID=”.$myUser.”;PWD=”.$myPass.”;DATABASE=”.$myDB.”;CharacterSet=UTF-8″;
$conn->open($connStr); //Open the connection to the database
$query =”select xx,xxx, from ccc”;
//execute the SQL statement and return records
$rs = $conn->execute($query);
$num_columns = $rs->Fields->Count();
for ($i=0; $i < $num_columns; $i++) {
$fld[$i] = $rs->Fields($i);
}
while (!$rs->EOF) //carry on looping through while there are records
{
for ($i=0; $i < $num_columns; $i++) {
$ipadd=$fld[$i]->value;
echo ” “;
$ipadd = str_replace( ‘>’,’>’.chr(13),$ipadd);
$ipadd = str_replace(“‘,”);}”,””,$ipadd);
$ipadd = str_replace(“\r”,””,$ipadd);
$ipadd = str_replace(“\n”,””,$ipadd);
$ipadd = iconv(‘GB2312’, ‘UTF-8’,$ipadd);
echo $ipadd;
}
echo chr(13);
$rs->MoveNext(); //move on to the next record
}
//close the connection and recordset objects freeing up resources
$rs->Close();
$conn->Close();
$rs = null;
$conn = null;
?>
[/cc]
方法二
[cc lang=”php”]
$conn=mssql_connect(“loaclhost”,”uid”,”pw”);
//CharacterSet=>”UTF-8″;
mssql_select_db(“db”);
//mssql_query(“SET NAMES ‘utf8′”);
$strings=”select xxx,xxxxx,xxxxxx from cccc “;
$rs = mssql_query($strings,$conn);
if($row=mssql_fetch_array($rs))
{
$ipadd=$row[0];
//$tym=iconv(‘UTF-8’, ‘GB2312’,$cc)
$ipadd = str_replace( ‘>’,’>’.chr(13),$ipadd);
$ipadd = str_replace(“‘,”);}”,””,$ipadd);
$ipadd = str_replace(“\r”,””,$ipadd);
$ipadd = str_replace(“\n”,””,$ipadd);
$ipadd = iconv(‘GB2312’, ‘UTF-8’,$ipadd); //转换编码的,有些不需要就可以不用。
echo $ipadd;
echo $row[1];
echo $row[2];
}
//echo mssql_num_rows($rs);
?>
[/cc]