我有什么:
我有一个名为 get_name()
的函数,如果在其参数中提供了现有的用户名和密码,它会从数据库返回用户名,并且它可以完美运行。代码:
function get_name($theusername, $thepassword){
$host = 'localhost';
$username = 'root';
$password = '';
$database = 'website';
$con = mysqli_connect($host, $username, $password, $database);
$theusername = mysqli_real_escape_string($con, $theusername);
$thepassword = mysqli_real_escape_string($con, $thepassword);
$thepassword = md5($thepassword);
$query = "SELECT * FROM members WHERE username = '{$theusername}' AND password = '{$thepassword}'";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_array($result);
return $row['name'];
mysqli_close($con);
}
我想要什么:
我想创建一个名为 attempt_login()
的函数,如果用户名和密码正确,则应返回 true
,否则应返回 false
。
DATABASE SCREENSHOT
有人可以帮我做这个吗?
最佳答案
您可以检查 $row['name'] 是否已设置。
不要使用return $row['name']
,而是使用return isset($row['name'])
。
isset
函数返回一个 bool 值
。
isset()
的 PHP
文档
http://php.net/isset
关于php - 如何使用 mysql 在 php 中创建 attemp_login() 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22643348/