我收到了这个错误:
警告:mysql_fetch_assoc()要求参数1为资源,布尔值在第70行的C:\ xampp \ htdocs \ core \ functions \ users.php中给出。
注册成功,登录成功;问题是它没有向我显示用户数据并给我这个错误。我是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']
答案 0 :(得分:0)
首先,您应该切换到msqli
,因为msql
在PHP 5.5.0中已弃用,并且正在PHP 7.0.0中删除
这是一个重复的问题,answer可以帮助您
<?PHP
$sql = 'some query...';
$result = mysql_query($q);
if (! $result){
throw new My_Db_Exception('Database error: ' . mysql_error());
}
while($row = mysql_fetch_assoc($result)){
//handle rows.
}
注意检查(!$ result) - 如果你的$ result是一个布尔值,它肯定是假的,这意味着有一个数据库错误,这意味着你的查询可能不好。 / p>
以下是来自msqli_fetch_assoc()
http://php.net/manual/en/mysqli-result.fetch-assoc.php
以下是msql_fetch_assoc()
被弃用的信息