-2

I've spent some time searching on the Internet and playing around with my code but I still can't figure out why I am getting this error message. Here is an excerpt from my code:

    } else {
        if (!empty($errors) && nexus_error($nexus)==false) {
            $message = "There were" . count($errors) . " errors in the form.";
        } if (!empty($errors) && nexus_error($nexus)) {
            $message = "There were" . count($errors) . " errors in the form.";
            $message .= "A user with the username" . $nexus . " already exists in the database."; 
        } if (empty($errors) && nexus_error($nexus)) { //***this line causes the error
            $message = "A user with the username" . $nexus . " already exists in the database."; 
        }
    }

The function nexus_error, by the way, is defined below:

function nexus_error($sel_nexus) {
    global $connection;
    $query = "SELECT * FROM person WHERE nexus={$sel_nexus}";
    $result_set = mysql_query($query, $connection);
    confirm_query($result_set);
    if (count(mysql_fetch_array($result_set)) != 0) {
        return true;    // bad
    } else {
        return false;  
    }
}

Any help would be great. Thanks for your time :)

nv39
  • 535
  • 2
  • 12
  • 21
  • Did you try [searching](http://www.google.com/search?q=Fatal+error%3A+Can't+use+function+return+value+in+write+context)? Or looking at any of those **Related** questions that Stack Overflow suggested when you composed your question (they're listed again on the right of this page)? The top hit on Google is [this](http://stackoverflow.com/questions/1532693/weird-php-error-cant-use-function-return-value-in-write-context) Stack Overflow question. – eggyal May 22 '12 at 15:13
  • where is the function definition of confirm_query($result_setuery)? – jugnu May 22 '12 at 15:14

2 Answers2

2
if (count(mysql_fetch_array($result_set)) != 0)

You can't count() a function return's value. You should store it in a variable before.

Samy Dindane
  • 17,900
  • 3
  • 40
  • 50
0

As Samy said the line in question is if (count(mysql_fetch_array($result_set)) != 0) {

The proper way to count the amount of results returned is mysql_num_rows() instead of counting, and your line could simply be this:

if (mysql_num_rows($result_set) != 0) {

Also, your code is currently inefficient in that the nexus_error($nexus) could be called 3 times on the same variable if it filters through to the last if statement (that's 2 unecessary queries), consider refactoring like so:

$nexusError = nexus_error($nexus);
 } else {
    if (!empty($errors) && $nexusError ==false) {
        $message = "There were" . count($errors) . " errors in the form.";
    } if (!empty($errors) && $nexusError) {
        $message = "There were" . count($errors) . " errors in the form.";
        $message .= "A user with the username" . $nexus . " already exists in the database."; 
    } if (empty($errors) && $nexusError) { //***this line causes the error
        $message = "A user with the username" . $nexus . " already exists in the database."; 
    }
}
Dunhamzzz
  • 14,682
  • 4
  • 50
  • 74
  • Hi, thank you. I took (both of) your suggestions. However, the error still persists. The error log is only showing me this one error. It is weird, even when I comment out the entire block of code, I still get the same error... – nv39 May 22 '12 at 15:26
  • Not sure what you mean by that; do you mean that the error is in a different part of my code which I did not post? – nv39 May 22 '12 at 15:29
  • Yeah it could be if you are getting the error when you uncomment it. – Dunhamzzz May 22 '12 at 15:31
  • You are right; it was something really stupid (renamed file but was playing with code in original file). Thanks for the great answer – nv39 May 22 '12 at 15:42