开发者

php error for registering a user

开发者 https://www.devze.com 2023-04-04 01:56 出处:网络
Im getting this error in a basic register script: Warning: mysql_result() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/func/user.func.php on line 23

Im getting this error in a basic register script:

Warning: mysql_result() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/func/user.func.php on line 23

The part of the register.php that's giving me the error is:

<?php

include('init.php'); // user.func.php is included in this file
include('template/header.php');
?>

<h3>Register</h3>

<?php

    // Typical $_POST stuff here, down the line the next line is where the error happenes.  Also, $register_email below is equal to $_POST['register_email'];

            if(user_exists($register_email)) { ***THIS FUNCTION IS WHERE THE PROBLEM IS.  THE ACTUAL FUNCTION IS DEFINED BELOW***
                $errors[] = 'That email has already been registered';
            }

The function from user.func.php that's giving me the error is:

function user_exists($email) {
    $email = mysql_real_escape_string($email);
    $query = mysql_query("SELECT COUNT(user_id) FROM users WHERE email = '$email'");
 开发者_JAVA技巧   return (mysql_result($query, 0) == 1) ? true : false; // ***THIS LINE RIGHT HERE***
}

Any ideas on what might be causing this error. It's an annoying error. Not the first time I've gotten that one.

UPDATE Thanks for the answers, I've tried each one and I'm getting the exact same error. Here's the full register.php so far:

<?php

include('init.php');
include('template/header.php');
?>

<h3>Register</h3>

<?php

if(isset($_POST['register_email'], $_POST['register_name'], $_POST['register_password'])) {
    $register_email = $_POST['register_email'];
    $register_name = $_POST['register_name'];
    $register_password = $_POST['register_password'];

    $errors = array();

    if(empty($register_email) || empty($register_name) || empty($register_password)) {
        $errors[] = 'All fields required';
    } else {
        echo 'OK';
        }
        if(filter_var($register_email, FILTER_VALIDATE_EMAIL) == false) {
            $errors[] = 'Email address is not valid';
        }
        if(strlen($register_email) > 255 || strlen($register_name) > 35 || strlen($register_password) > 35) {
            $errors[] = 'Ayo, quit tampering with the html';
        }
        if(user_exists($register_email)) {
            $errors[] = 'That email has already been registered';
        }
}

if(!empty($errors)) {
    foreach($errors as $error) {
        echo $error.'<br />';
    }
} else {

    }

?>


Now, I must say first that I'm not a mysql specialist and I normally use a DB class (so should you.) But if you are saying that return (mysql_result($query, 0) == 1) ? true : false; line is giving you an error. It means that the line above is not working. Meaning that it is not returning a resource.

You should first debug your function..

function user_exists ($email) {
    $email = mysql_real_escape_string($email);
    if (!mysql_select_db("users")) {
        echo 'Could not select "users" DB.<br />Error: ' . mysql_error();
    }
    $query = mysql_query("SELECT COUNT(user_id) AS `count` FROM `users` WHERE `email` = '$email'");
    echo 'The count is currently: '$query['count'];
    // return (mysql_result($query, 0) == 1) ? true : false;
}

If it says that it couldn't select the users DB. Then the problem is in your connections. As I said, I'm no pro. But you should probably connect it like this:

$conn = mysql_connect('localhost', 'mysqluser', 'mypass');

Now you can try this:

function user_exists ($email) {
    global $conn;
    $email = mysql_real_escape_string($email);
    if (!mysql_ping($conn)) {
        echo 'Could not ping the mysql. Connection is lost probably :(';
    }
    $query = mysql_query("SELECT COUNT(user_id) AS `count` FROM `users` WHERE `email` = '$email'", $conn);
    echo 'The count is currently: ' . mysql_result($query, 0);
    // return (mysql_result($query, 0) == 1) ? true : false;
}

If the code is been debugged and connection is AWESOME! Then:

function user_exists ($email) {
    global $conn;
    if ($email) {
        $query = mysql_query("SELECT COUNT(user_id) AS `count` FROM `users` WHERE `email` = '$email'", $conn);
        if (mysql_result($query, 0)) {
            return true;
        }
    }  
    return false;
}

Or:

function user_exists ($email) {
    global $conn;
    if ($email) {
        $query = mysql_query("SELECT COUNT(user_id) AS `count` FROM `users` WHERE `email` = '$email'", $conn);
        if ($result = mysql_fetch_array($query)) {
            if ($result['count'] == 0) {
                return true;
            }
        }
    }  
    return false;
}


If you look in the manual, mysql_query() can return a ressource (thats what you expect) OR FALSE if an error occur.

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error. For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.


Change to:

function user_exists($email) {
    $email = mysql_real_escape_string($email);
    $query = mysql_query("SELECT email FROM users WHERE email = '$email'");
    if (false === $query) return false;
    return (mysql_num_rows($query) == 1);
}


use

function user_exists($email) {

    if(isset($email){
    $email = mysql_real_escape_string($email);
    $query = mysql_query("SELECT COUNT(user_id) FROM users WHERE email = '$email'");
    $result = mysql_result($query,0);
    if($result ===false) {
    //error occur with the sql statement 
     //handel the error
    }
    else
    return ($result == 1) ? true : false; // ***THIS LINE RIGHT HERE***
}
}


function user_exists($email) {
  $email = mysql_real_escape_string($email);
  $query = mysql_query("SELECT COUNT(user_id) FROM users WHERE email = '$email'");
  //return (mysql_result($query, 0) == 1) ? true : false; // ***THIS LINE RIGHT HERE***
  if( $query ) return ( mysql_result($query, 0) != "" ) ? true : false;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消