Hi all,

I'm trying to run an update query using the following code:

<?php
    require_once('myConnect/connect.php');

    $email = mysqli_real_escape_string($dbc, $_GET['e']);
    $ac = mysqli_real_escape_string($dbc, $_GET['ac']);

    $checkQuery = "SELECT subscribed FROM myUsers WHERE email='$email'";
    $checkQueryResult = @mysqli_query($dbc, $checkQuery);
    $row = mysqli_fetch_assoc($checkQueryResult);

    if ($row['subscribed'] == 1) {
        echo '<p><b>This account has already been activated!</b></p>';
    } else {     
        $updateQuery = "UPDATE myUsers SET subscribed=1, dateSubscribed=NOW() WHERE email='$email' AND ActivateCode='$ac'";
        $updateQueryResult = @mysqli_query($dbc, $updateQuery);

        if($updateQueryResult) {
            echo '<p>Thank you! Your subscription is complete!</p>';
        } else {
            echo '<p>Subscription failed!</p>';
        }
        mysqli_free_result($updateQueryResult);
    }

    mysqli_free_result($checkQueryResult);
    mysqli_close($dbc);
?>

I am getting the following output:

Thank you! Your subscription is complete!

Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given in filepath/confirm.php on line 22

and the table is updated with the correct values.
From what I've read, this error is generated when the query fails. But how can that be so when the query runs and updates the table?

I'm confused.

Recommended Answers

All 15 Replies

I would do this

$checkQueryResult = mysqli_query($dbc, $checkQuery)or die(mysqli_error($dbc));

The @ in front of @mysqli_query supresses errors, you don't want that there when you are debugging.

Setting this:

or die(mysqli_error($dbc));

Will show you any errors that are generated during the query as well.

Good call. I forgot that part but it's showing the same output regardless.

hmm, do you have the column 'dateSubscribed' set to datetime or timestamp (can't remember which one)? Also, is that table getting updated?

It's set to DATE. I've another DATE column that I insert into using NOW() in another script and that one doesn't come up with any errors.
And yes, the table updates. Hence my confusion at the error message.

I even get the error etc with CURDATE()

Column          Type           Null        Default      Comments
userID          mediumint(8)    No           
username        varchar(30)     No           
email           varchar(80)     No           
ActivateCode    varchar(90)     No           
dateCreated     date            Yes        NULL      
subscribed      tinyint(1)      No           
dateSubscribed  date            Yes        NULL      

IndexesDocumentation
Keyname Type    Unique  Packed  Column  Cardinality Collation   Null    Comment
PRIMARY BTREE   Yes No  userID  1   A   No  

Well, something is returning FALSE, somewhere.

Are you using this on the end of both the queries?:

or die(mysqli_error($dbc));
Member Avatar for diafol

<unhelpful and off-topic> Your title suggests that you are running on a quantum computer :)

It's a bit out there :)

My ex-girlfriend used to tell me she loved me and hated me at the same time. It was a quantum relationship, it got deleted :-))

I've had a thought though (it happens). If mysqli_free_result gets rid of the object then does that mean that it isn't needed with UPDATE? Or do I have that all wrong?

Regarding mysqli_query:

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

Click Here

Conclusion: Back to reality. The cat lives!

Yup, your right. It doesn't need it with UPDATE. UPDATE returns a boolean which does not need to be freed. That actually crossed my mind for 2 seconds, but then I thought I was just being dumb.

Member Avatar for diafol
mysqli_free_result($updateQueryResult);

I think you've found the answer. You don't need to do this on an update as an update only returns true/false which is only sensible - there's no object to return. The select however should return data.

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.