-4

我正在通过 php 建立一个用户登录系统 我建立了数据库建立了连接 建立了表格,获取用户名和密码并存储在变量中

/// 数据库连接代码///

<?php
 mysql_connect('localhost','root','#########'); 
 mysql_select_db('scholarships_system');
 ?>

/////将数据转化为变量/////

 if(empty ($_POST)=== false){
$username = $_POST['username'];
$password = $_POST['password'];
if(empty($username)=== true || empty($password)=== true )
{
$errors[] = 'You need to enter a username and password';
}
else if (user_exists($username)=== false)
{$errors[] = 'we cant find that username have you registerd ?';
}
}

/////// 功能代码 ////

function user_exists($username){
return (mysql_result(mysql_query(" SELECT COUNT (`user_id`) FROM `users_login` WHERE `username` = '$username'"),0) == 1) ? true : false;
    }

///如果我调用函数来检查错误发生////

Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\wamp\www\login and registry\core\functions\users.php on line 6

////第6行是////

return (mysql_result(mysql_query(" SELECT COUNT (`user_id`) FROM `users_login` WHERE `username` = '$username'"),0) == 1) ? true : false;
        }
4

1 回答 1

1

你需要像这样使用:-

function user_exists($username) {
   $count = mysql_result(mysql_query(" SELECT COUNT (`user_id`) FROM `users_login` WHERE `username` = '$username'"),0);
   return $count == 1 ? true : false;
}
于 2013-09-05T11:15:51.100 回答