Help please!!

+1 Ali Khiti · August 29, 2014
Hi everyone, I've got this code from somewhere in the internet and I don't know why it doesn't work for me. Here's the erorrs it gives me:

Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Welive\functions.php on line 24

Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Welive\functions.php on line 31


Post a Reply

Replies

Oldest  Newest  Rating
+1 Franz Schmidt · August 30, 2014
No problem :D
0 Ali Khiti · August 29, 2014
Here's the register.php



<?php
$page_title = "Welive";
include 'html/header.php';

?>

<?php
include 'functions.php';
if (empty($_POST) === false) {
    $required_fields = array('fname', 'username', 'email', 'password', 'password2');
    foreach ($_POST as $key => $value) {
        if (empty($value) && in_array($value, $required_fields) === true ) {
            $errors[] = 'Please, fill in all fields!';
            break 1;
        }
    }

    if (empty($errors) === true) {
        if (username_exists($_POST['username']) === true) {
            $errors[] = 'Sorry your username already exists, choose another one.';
        }
        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 long';
        }
        if ($_POST['password'] !== $_POST['password2']) {
            $errors[] = 'Your passwords don\'t match';
        }
        if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
            $errors[]= 'Your email is not valid, check it again!';
        }
        if(email_exists($_POST['email']) === true)
            $errors[]= 'Sorry your email is already taken, choose another one.';
        }
}
if (empty($_POST) ===false && empty($errors) === true){
    $register_data = array(
        'username'         => $_POST['username'],
        'full_name'     => $_POST['fname'],
        'email'         => $_POST['email'],
        'password'         => $_POST['password']
        );
    register_user($register_data);
    exit();
}
else if (empty($errors) === false){
    echo output_errors($errors);
}
?>


<div id="login">
<h2 style="color: white;">Register your account or <a href="index.php">login</a></h2>
<div id="login_panel">
<form action="register.php" method="post" accept-charset="utf-8">
<div class="login_fields">
<div class="field">
<input type="text" name="fname" value="" id="email" tabindex="1" placeholder="Your Full Name " />
</div>
<div class="field">
<input type="text" name="username" value="" id="email" tabindex="2" placeholder="Choose a username" />
</div>
<div class="field">
<input type="text" name="email" value="" id="email" tabindex="3" placeholder="Enter your email adress" />
</div>
<div class="field">
<input type="password" name="password" value="" id="password" tabindex="4" placeholder="Choose a password" />
</div>
<div class="field">
<input type="password" name="password2" value="" id="password" tabindex="5" placeholder="Choose a password" />
</div>
</div>

<div class="login_actions">
<input type="submit" class="btn primary" name="reg" tabindex="6" value="Register">
</div>
</div>
<?php include 'html/footer.php'; ?>


0 Ali Khiti · August 29, 2014
Here's the functions.php



<?php
require_once('config.php');
$errors = array();
$con = mysqli_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_NAME);

if ( mysqli_connect_errno() )
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}



function register_user($register_data){
    array_walk($register_data, 'array_sanitize');
    $register_data['password'] = md5($register_data['password']);
    $fields =   '`' . implode('`, `', $register_data)  . '`';
    $data = '\'' . implode('\' , \'' , $register_data) . '\'';
    mysql_query("INSERT INTO `users` ($fields) VALUES ($data)");
}

function username_exists($username){
    $username = sanitize($username);
    $query = mysql_query("SELECT count(`user_id`) FROM `users` WHERE `username` = '$username'");
    return(mysql_result($query, 0)) ? true : false ;

}

function email_exists($email){
    $email = sanitize($email);
    $query = mysql_query("SELECT count(`user_id`) FROM `users` WHERE `email` = '$email'");
    return(mysql_result($query, 0)) ? true : false ;

}


function output_errors($errors){
    return '<ul><li>' . implode('</li><li>', $errors) . '</li></ul>';
}

function sanitize($data){
    return mysql_real_escape_string($data);
}

function array_sanitize(&$item) {
    $item = mysql_real_escape_string($item);
}

?>

0 Franz Schmidt · August 30, 2014
I think I got your problem. Look at this the problem is the query.
http://stackoverflow.com/questions/19736842/php-mysql-mysql-result-expects-parameter-1-to-be-resource-boolean-given-in
0 Ali Khiti · August 30, 2014
Yhanks dude
  • 1

PHP

107,076 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators