Hello,

Can someoneone tell me when you prepare() a statement (in my case an update statement) and then execute() it, i then check using rowCount() after the update query (see code below) if rowCount() > 0 if it is then a match was found and update took place, but i then got an else statement, now my question is do i need the else statement ? , i think its better explained in code below, i commented the code and commented in more depth the area of code i am talking about.

Please ignore the UPDATE SQL in the prepare statement as i think its wrong and will deal with later.

// check if key is set and alphanumeric and equals 32 chars long
// we use sha1 so it will always be 40 chars long.
if(isset($_GET['key']) && ctype_alnum($_GET['key']) && strlen($_GET['key']) == 40){
$key = trim(msqls($_GET['key']));
}

// if key isset and valid
if(isset($key)){


try {
    // connect to database
    $dbh = sql_con();

    // checke if activation key matches and user_uid matches
    $stmt = $dbh->prepare("
            SELECT
              users_status.user_uid,
              users_status.user_activation_key
            FROM
              users_status
            JOIN
              users
            ON
              users_status.user_activation_key = ?
            AND
              users_status.user_uid = users.user_uid LIMIT 1");

    // execute query
    $stmt->execute(array($key));

    // if row count greater than 0 then match found
    if ( $stmt->rowCount() > 0 ) {

        // user verified; we now must update users status in users table to active = 1
        // and set the user_activation_key in the users_status to NULL
        $stmt = $dbh->prepare("
            UPDATE
              users.user_status,
              users_status.user_activation_key
            SET
              user_status = ".USER_STATUS_ACTIVE.",
              user_activation_key = NULL
            JOIN
              users
            ON
              users_status.user_activation_key = ?
            AND
              users_status.user_uid = users.user_uid LIMIT 1");

        // execute query - THIS IS THE UPDATE EXECUTE QUERY
        $stmt->execute(array($key));

        // I AM NOW CHECKING IF THE RETURNED RESULT IS GREATER THAN 0; IF IT IS THEN MATCH FOUND AND UPDATE WAS SUCCESFUL
        if ( $stmt->rowCount() > 0 ) {

            // update was succesful .. DO WHATEVER I WANT HERE

        } else {
            // update not sucessful
            // THIS IS THE BIT IM CONFUSED WITH; DO I NEED THIS ?

            // IF RETURNED RESULT IS 0 THEN I GUESS THAT MEANS THERE WAS NOT AN ERROR IN SQL SYNTAX BUT
            // CONDITION IN SQL STATEMENT COULD NOT BE MATCHED ? IS THAT CORRECT WHAT I AM THINKING ?
            // IF I AM CORRECT THEN OBVIOUSLY I WILL DISPLAY A MESSAGE TO USER AND EXIT HERE;
            // AS IF I AM THINKING RITE ANY SYNTAX ERROR WOULD BE CAUGHT BY CATCH BLOCK AND THIS ELSE STATEMENT
            // MEANS COULD NOT UPDATE BECAUSE NO MATCH IN UPDATE QUERY COULD BE FOUND ?
        }


    } // else no match found
    else {

        // no match found invalid key

    }
} // if any errors found log them and display friendly message
catch (PDOException $e) {
    ExceptionErrorHandler($e);
    require_once($footer_inc);
    exit;
}

} else {

// else key not valid or set
echo '<h1>Invalid Activation Link</h1>';

$SiteErrorMessages =
"Oops! Your account could not be activated. Please recheck the link in your email.
The activation link appears to be invalid or has now expired.<br /><br />
Please request a new one <a href='/member/resend-activation-email'>here</a>.
If the problem persists please contact us <a href=\"/contact\">here</a>.";

SiteErrorMessages();

include($footer_inc);
exit;

}

Thanks for any help as always,
phplover

Member Avatar for diafol

whether or not you need the 'else' is up to you. If the user is expecting an update and doesn't get one, surely they should be informed? Otherwise Joe Bloggs will be merrily ignorant of all his updates not taking hold.

Hi ardav,

I know i will have to inform them like you say, it's me not explaining things very well, nothing new there to be honest.

What i meant was does the else statement basically mean cannot update because a match was not found and/or a possible syntax error or some other error ?

I think it means what i put in bold as if i am thinking correctly any syntax error or any error that makes the update fail will be caught in the catch block on which i have an exception-error-handler to log the error to file

Thanks you,
phplover

Member Avatar for diafol

In your production site you should trap all errors anyway. The else will report what you tellit to report. It should be triggered by syntax error and 0 records. I think :)

Thanks for replying,

This is my reason to my questions, i want to make sure i am understanding my own code correctly,

I know i shold trap all errors hence why my database queries etc are in a try and catch block and i have an excepton error handler function to log any exceptions (errors) caught by the catch block to a file.

I just want to make sure that i am thinking correctly and understanding my code properly hence my post.

I will see if anyone else replied that could explain if i am thinking correctly in what i have asked

Thank you,
phplover

Basically, if I understand your structure right. the else after the update will never be called. The join is the same as in the select statement before it. If it is found there, then it will be found again, unless you just manually deleted a record.

Thanks Pritaeas, +1

I have done some intense reading yesterday and understand now what i am doing, i forgot to mark solved, i also removed the select statement as i did not need it, All working and sorted now.

Thanks everyone!
phplover

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.