1

Here is my code:

$token   = $_GET['validate_activate']; // it's something like this "98j34f0j043f094f98325r"
$user_id = $_GET['user_id'];

// db connection here
$stmt = $dbh->prepare("DELETE FROM activate_token WHERE token = :token");
$stmt->execute(array(':token' => $token));

if ( /* deleting is success */ ) {
    // activating the user
} else {
    // your token is invalid
}

Well how can I make a correct condition for that if statement?

Shafizadeh
  • 9,960
  • 12
  • 52
  • 89

3 Answers3

0

With the help of rowCount();

PDOStatement::rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

if ( $stmt->rowCount() ) {
    // activating the user
} else {
    // your token is invalid
}
e4c5
  • 52,766
  • 11
  • 101
  • 134
  • hello @Drew is there a bug in newer versions? Haven't used PHP for a couple of years – e4c5 Jul 11 '16 at 23:03
  • If it works on your deployment, and well tested, your method is what I would go with. It is just when you push your code around, it is dependent on the setup in action. From the manual: [not guaranteed](http://php.net/manual/en/pdostatement.rowcount.php) – Drew Jul 11 '16 at 23:04
0

This should work, the pdo statement should return True or False if the delete query was successful

if($stmt->execute(array($token = $_GET['validate_activate']))) {
     // activating the user
} else {
    // your token is invalid
}
Allen Butler
  • 337
  • 3
  • 12
0

$stmt->execute returns a bool result, true if the sql query was successful so just use the result of that in your if statement:

$stmtWorked = $stmt->execute(array($token));
if ($stmtWorked) {
    // activating the user
} else {
    // your token is invalid
}

Also you can use mysqli_stmt_affected_rows() to count for many rows were deleted if you need to know a specific amount.

chickenwingpiccolo
  • 193
  • 1
  • 3
  • 13