两种途径:
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=$hostnamedatabase=$dbname"
$conn
=
new
PDO
($dsn,$username,$pwd)
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION)
echo
"mssql
database
connnection
sucessed!"
php有专门的sql server *** 作函数,举个简单的例子,是查询的
$serverName = "localhost" //数据库服务器地址$uid = "root" //数据库用户名
$pwd = "123456" //数据库密码
$connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Database"=>'databasename')
$conn = sqlsrv_connect( $serverName, $connectionInfo)
if( $conn == false){
echo "连接数据库失败!"
die( print_r( sqlsrv_errors(), true))
}
$sql = "select * from user"
$query = sqlsrv_query( $conn, $sql , array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ))
$num_rows = sqlsrv_num_rows($query)
if($num_rows > 0){
while ($row = sqlsrv_fetch_array($query)){
echo $row['aaaa']
}
}
其它的 *** 作也同理,举一反三
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)