Hello forum! I was tinkering with a login script and I came accross the following error: Warning: mysql_result() expects parameter 1 to be resource, boolean given in /home/content/79/9398479/html/abyss/dio.php on line 60

//Code here:

<?php
$sqlError = 'We are experiencing very temporary downtime, please refresh or try again in 10 minutes.';
$sqlCon = mysql_connect('database', 'username', 'password');

if (!$sqlCon) {
  die($sqlError);
}

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

mysql_select_db('dbName', $sqlCon) or die($sqlError);


function userActive($username) {
  $username = cleanup($username);
  $query = mysql_query("SELECT COUNT(`ID`) FROM `users` WHERE `username` = '$username' AND `active` == 1"); // <-line 60 
  return (mysql_result($query, 0) == 1) ? true : false;
}

Any help with this code would be appreciated, as I am truly stumped and cannot figure out what is wrong :/

See the PHP documentation for this function here: http://us2.php.net/mysql_query

Per the documentation, it states that mysql_query will return a resource upon successful execution of a SELECT query, but will return a boolean FALSE upon failure of such a query. In your instance, mysql_query is returning false because MySQL cannot process your query. Looking at the query, I think the issue is that you are using "==" instead of "=" when evaluating the value of active.

Try this...

function userActive($username) {
    $username = cleanup($username);
    $query = mysql_query("SELECT COUNT(`ID`) FROM `users` WHERE `username` = '$username' AND `active` = 1"); // <-line 60 
    return (mysql_result($query, 0) == 1) ? true : false;
}

Or better yet, check the outcome of $query before trying to get mysql_result...

function userActive($username) {
    $username = cleanup($username);
    $query = mysql_query("SELECT COUNT(`ID`) FROM `users` WHERE `username` = '$username' AND `active` = 1"); // <-line 60 
    if ($query === false) {
        // Look at the MySQL error to help you debug...
        print mysql_error();
        die; // or return false;
    }
    else
        return (mysql_result($query, 0) == 1) ? true : false;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.