我得到這個錯誤:警告:mysql_fetch_assoc()預計參數1是資源,鑑於布爾
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\core\functions\users.php on line 70.
它註冊成功,併成功登錄;問題是它沒有顯示我的用戶數據,並給我這個錯誤。我是PHP新手。
這裏是我的代碼users.php:
function user_data($user_id) {
$data = array();
$user_id = (int)$user_id;
$func_num_args = func_num_args();
$func_get_args = func_get_args();
if ($func_num_args > 1) {
unset($func_get_args[0]);
$fields = '`' . implode ('`, `', $func_get_args) . '`';
$data = mysql_fetch_assoc(mysql_query("SELECT $fields FROM `users` WHERE `user_id` = $user_id"));
return $data;
}
}
function login($username, $password){
$user_id = user_id_from_username($username);
$username = sanitize($username);
$password = md5($password);
return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username`='$username' AND `password`='$password'"), 0)==1) ? $user_id : false;
}
function logged_in() {
return (isset($_SESSION['user_id'])) ? true : false;
}
function user_exists($username) {
$username = sanitize($username);
return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` ='$username' "), 0) == 1) ? true : false;
}
function user_active($username) {
$username = sanitize($username);
return(mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND `active` =1"), 0) == 1) ? true : false;
}
而且我register.php
<?php
include 'core/init.php';
include 'overall/headerr.php';
if (empty($_POST) === false) {
$required_fields = array('username', 'password', 'password_again', 'first_name');
foreach($_POST as $key =>$value) {
if(empty($value) && in_array($key, $required_fields) === true) {
$errors[] = 'Fields marked with an asterisk required';
break 1;
}
}
if (empty($errors) === true){
if (user_exists($_POST['username']) === true) {
$errors[] = 'Sorry, the username \'' . $_POST['username'] . '\' is already taken.';
}
if (preg_match("/\\s/", $_POST['username'])== true) {
$errors[]='Your username must not contain any spaces.';
}
if (strlen($_POST['password']) < 6) {
$errors[] = 'Your password must be at least 6 characters';
}
if ($_POST['password'] !== $_POST['password_again']) {
$errors[] = 'Your password do not match';
}
}
}
?>
<h1>Register</h1>
<?php
if (isset($_GET['success']) === true && empty ($_GET['success']) === false) {
echo 'You\'ve been registered successfully!';
}else{
if (empty($_POST) === false && empty ($errors) === true) {
$register_data = array(
'username' => $_POST['username'],
'password' => $_POST['password'],
'first_name' => $_POST['first_name'],
'contact' => $_POST['contact'],
'information' => $_POST['information']
感謝@gmaniac – John
上
msql_fetch_assoc()
的信息你歡迎,高興它有幫助。如果它解決了你的問題,請接受 – gmaniac