0

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.

Edited by Tinnin

3
Contributors
15
Replies
37
Views
4 Years
Discussion Span
Last Post by diafol
0

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.

0

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

0

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

Edited by pixelsoul

0

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.

0
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  
0

Well, something is returning FALSE, somewhere.

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

or die(mysqli_error($dbc));

Edited by pixelsoul

0

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

Edited by diafol

0

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 :-))

0

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?

0

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

0

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.

Edited by pixelsoul

0
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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.