简体   繁体   中英

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.

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. 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.

function user_exists($conn, $username) {
   ...

And also, you need to call user_exists(...) somewhere to make it all work. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM