我正在使用sqlsrv_num_rows
来检查数据库中是否存在用户。
当我在数据库中运行查询时,我得到1个结果,但在PHP中我什么也没得到(echo
什么都不打印)。为什么会这样?
$query = "SELECT TOP 1 id, tourOp FROM users WHERE (valid = 1) AND (email = '".trim($_POST['email'])."') AND (password = '".trim($_POST['password'])."')";
$stmt = sqlsrv_query( $conn, $query);
echo "num: ".sqlsrv_num_rows( $stmt );
if (!sqlsrv_num_rows( $stmt )) {
return (false);
} else {
}
示例查询
SELECT TOP 1 id, name FROM users WHERE (valid = 1) AND (email = 'roi@some_email.com') AND (password = '8521')
我正在使用PHP和MSSQL。
答案 0 :(得分:1)
说明:
sqlsrv_num_rows()
需要客户端,静态或键集游标,如果您使用前向游标或动态游标(默认游标为前向游标),它将返回false
。使用附加的sqlsrv_query()
参数执行$options
,并使用"Scrollable" => SQLSRV_CURSOR_KEYSET
设置适当的光标类型sqlsrv_query()
同时执行语句准备和语句执行,可用于执行参数化查询。sqlsrv_has_rows()
。示例,根据您的代码:
<?php
$query = "
SELECT TOP 1 id, tourOp
FROM users
WHERE (valid = 1) AND (email = ?) AND (password = ?)";
$params = array(trim($_POST['email']), trim($_POST['password']));
$options = array("Scrollable" => SQLSRV_CURSOR_KEYSET);
$stmt = sqlsrv_query( $conn, $query, $params, $options);
if ($exec === false){
echo print_r( sqlsrv_errors());
echo "<br>";
return (false);
}
$count = sqlsrv_num_rows($stmt);
if ($count === false) {
echo print_r( sqlsrv_errors());
echo "<br>";
return (false);
} else {
echo "num: ".$count;
}
?>
注意:
请勿以纯文本格式发送用户凭据。