PHP连接sqlserve与sqlserve增删查改语句实战

  • A+
所属分类:MySQL

PHP连接sqlserve:

1
2
3
4
5
6
7
8
9
10
11
$serverName = "xx.xx.xx.xx"; //数据库服务器地址
$uid = "xx"; //数据库用户名
$pwd = "xxxxxxxx"; //数据库密码
$connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Database"=>"DB");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if ($conn=== false) {
die( print_r( sqlsrv_errors(), true) ); //连接失败
} else {
echo "success
";//连接成功
}

PHP中使用sqlserve语句查询数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//提交成功
$sqlsqlsrv = "SELECT * FROM [DB].[dbo].[T_Appid] where Appid = '$Appid'";//[DB].[dbo].[T_Appid] 是我的表 Appid是我的字段
$stmt = sqlsrv_query($conn, $sqlsqlsrv);
if ($stmt === false) {
die( print_r( sqlsrv_errors(), true) );//连接失败

} else {
while ($rowsqlsrv = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
echo $rowsqlsrv['name'] ;//获取当前条件查询到的条数中的 name字段的值
}
echo "success
";
}
sqlsrv_free_stmt($stmt);

PHP中使用sqlserve语句更新数据库

sqlsrv_rows_affected()方法可以获取到更新的条数.0为没有数据可以更新 ,false为更新失败 还有一个是返回-1.我忘记是什么了

1
2
3
4
5
6
7
8
9
$sql = "UPDATE [DB].[dbo].[T_Appid] SET Con= 1 WHERE Appid = '$Appid'";
$stmt = sqlsrv_query($conn, $sql);
$row = sqlsrv_rows_affected($stmt);//
if($row==0){
echo 400003;return;/*数据为空,没有查询条件的这条数据*/

}else {
/*更新成功*/
}

PHP中使用sqlserve语句添加

1
2
3
$sqls = "INSERT INTO [DB].[dbo].[T_GetCon] (Con,Appid) values ('1','21213')";
$stmtr = sqlsrv_query($conn, $sqls);
$row = sqlsrv_rows_affected($stmtr);

PHP中使用sqlserve删除

1
2
3
$sqls = "DELETE FROM [DB].[dbo].[T_Appid] where Appid = '$Appid'";
$stmtr = sqlsrv_query($conn, $sqls);
$row = sqlsrv_rows_affected($stmtr);
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: