我正在学习编写一个从SQL server获取user_status的函数。我通常使用mysqli数据库并在PHP中编写代码:
function fetchuserStatus($username,$password){
global $mysqli;
$stmt = $mysqli->prepare("SELECT user_status from table_name where username = ? AND password = ?");
$stmt->bind_param("ss",$username,$password);
$stmt->execute();
$stmt->bind_result($userstatus);
$stmt->store_result();
$stmt->fetch();
$stmt->close();
return $userstatus;
}
现在我想为SQL SERVER编写相同内容。我怎样才能做到这一点?因为我与SQL SERVER建立连接,这是为sql server编写代码的最佳方法。
注意:我使用的是PHP 7.1
提前致谢。
答案 0 :(得分:0)
查看PHP sql server文档http://php.net/manual/en/book.sqlsrv.php
但是如果我们要把它翻译成sql server。
function fetchuserStatus($username,$password){
$conn = sqlsrv_connect('localhost', array("Database"=>"USERS_TBL", "UID"=>"dbuser", "PWD"=>"dbpassword");
if ($conn) {
$sql = "SELECT user_status from table_name where username = ? AND password = ?");
$parameters = array($username,$password);
$stmt = sqlsrv_query($conn, $sql, $parameters);
if(sqlsrv_has_rows($stmt) === true) {
sqlsrv_fetch($stmt);
/*
Use results however you want
You can get results using sqlsrv_get_field($stmt, 0);
0 is the index of the column on your select, if you selected something after user_status then that column would be 1
*/
}
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
}
}