[英]Return a result from an sql query
I am trying to use the SQLSVR function to return a true or false from a query, i was using ODBC_connect and ODBC_results. 我正在尝试使用SQLSVR函数从查询中返回true或false,我正在使用ODBC_connect和ODBC_results。
This was my original script: 这是我的原始脚本:
function user_exists($username)
{
$result = odbc_exec (odbc_connect("book", "", ""), "select count ('user_id') FROM [User] WHERE username = '$username'");
return (odbc_result($result, 1) ? true : false);
}
But trying to use the SQLSVR function im not sure where to go. 但是尝试使用SQLSVR函数无法确定该去哪里。 This is what i have so far. 这是我到目前为止所拥有的。
function user_exists($conn, $username) {
$sql =("select count ('user_id') FROM [User] WHERE username = '$username'");
$stmt = sqlsrv_query( $conn, $sql);
$name = sqlsrv_get_field( $stmt, 0);
If ($name = 1) {
Return True;
}
else{
Return False;
}
} }
You need to pass $conn
to user_exists()
otherwise it will not be visible in function body. 您需要将$conn
传递给user_exists()
否则它将在函数主体中不可见。
function user_exists($conn, $username) {
...
And also, you need to call user_exists(...)
somewhere to make it all work. 而且,您需要在某个地方调用user_exists(...)
以使其全部正常工作。 Putting function method is not equivalent of calling it. 放置函数方法并不等同于调用它。
EDIT 编辑
This code is redundant: 此代码是多余的:
$test = sqlsrv_has_rows($result);
return (($test == true) ? true : false);
as it is sufficient to just: 因为只需:
return sqlsrv_has_rows($result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.