php怎样连接sqlserver数据库
1个回答
展开全部
我用的PHP版本是php5.6.12,mssql数据库版本为2008
两种途径:
1、直接连接
$servname="CAPTAINHERO567";
$conninfo=array( "Database"=>"netdata", "UID"=>"lxz2005", "PWD"=>"831140");
$conn=sqlsrv_connect($servname, $conninfo);
$sql="select * from Pinfo";
$db=sqlsrv_query($conn, $sql);
while($row=sqlsrv_fetch_array($db))
{
echo("<div>".iconv("GB2312","UTF-8",$row["Pname"])."</div>");
}
2、使用PDO抽象数据层连接
$hostname = "192.168.1.100";
$dbname = "Northwind";
$username = "sa";
$pwd = "pwd100";
$dsn="sqlsrv:Server=$hostname;database=$dbname";
$conn = new PDO ($dsn,$username,$pwd);
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
echo "mssql database connnection sucessed!";
两种途径:
1、直接连接
$servname="CAPTAINHERO567";
$conninfo=array( "Database"=>"netdata", "UID"=>"lxz2005", "PWD"=>"831140");
$conn=sqlsrv_connect($servname, $conninfo);
$sql="select * from Pinfo";
$db=sqlsrv_query($conn, $sql);
while($row=sqlsrv_fetch_array($db))
{
echo("<div>".iconv("GB2312","UTF-8",$row["Pname"])."</div>");
}
2、使用PDO抽象数据层连接
$hostname = "192.168.1.100";
$dbname = "Northwind";
$username = "sa";
$pwd = "pwd100";
$dsn="sqlsrv:Server=$hostname;database=$dbname";
$conn = new PDO ($dsn,$username,$pwd);
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
echo "mssql database connnection sucessed!";
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询