0

I have that PDO query to check for duplicate txn_id in my database. At the moment my database has NO record.

Everytime I run my ipn script on paypal sandbox I keep getting the email saying Duplicate Transaction ID. I dont get it. I have no record in my database so the transaction should have gone though.

Can anyone spot an error please.

$this_txn = $_POST['txn_id'];
$stmt = $conn->prepare("
SELECT txn_id FROM Transactions 
WHERE txn_id =:txn_id LIMIT 1
");
$stmt->bindParam('txn_id',$this_txn);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC); 
if ($rows > 0) {
    $messagerr = "Duplicate Transaction ID.";
    mail("example@test.com", "Duplicate txn_id in the IPN system", $messagerr, "From:        test@example.com" );
    exit(); 
} 
Angel
  • 15
  • 6
  • 1
    `$rows > 0` this is not correct, $rows is not number to compare it with zero, see this: http://stackoverflow.com/questions/460010/work-around-for-php5s-pdo-rowcount-mysql-issue –  Aug 08 '13 at 14:47
  • `fetchAll` doesn't return an int to compare against `> 0`. http://php.net/manual/en/pdostatement.fetchall.php – Aaron W. Aug 08 '13 at 14:48
  • use `$stmt->num_rows` – Pascamel Aug 08 '13 at 14:48
  • Are you even a PHP coder or you just found a script? Judging from other answers and comments you're making, it looks like you took someone's script or found it online. The part where you check number of rows is not the only problem of your code. – N.B. Aug 08 '13 at 15:32
  • @N.B. this is first time trying to create shopping cart and having to use paypal `IPN` also am a little new to PDO too. – Angel Aug 08 '13 at 23:46

3 Answers3

1

You're trying to compare an array to an integer/false. Instead, check the count of the array:

if (count($rows) > 0) {

You can also check rowCount:

if($stmt->rowCount() > 0)

Edit: This is the full code, and will also give you more information on what IDs are showing as duplicate:

$stmt->execute();
if ($stmt->rowCount() > 0) {
    $messagerr = "Duplicate Transaction ID. There are ".$stmt->rowCount() ." rows";
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); 
    foreach($rows as $row){
        $messageerr .= "\n TxnId = ".$row['txn_id'];
    }
    mail("example@test.com", "Duplicate txn_id in the IPN system", $messagerr, "From:        test@example.com" );
    exit(); 
} 
aynber
  • 22,380
  • 8
  • 50
  • 63
  • Yes, if you use the rowCount approach. If you use count($rows), then you need to keep the fetchAll line. – aynber Aug 08 '13 at 15:00
  • I made the edit. This will also give you more information on what is being duplicated. – aynber Aug 08 '13 at 15:25
1

Use stmt->rowCount() > 0 instead, fetchAll returns an array

Edit

Change:

$rows = $stmt->fetchAll(PDO::FETCH_ASSOC); 
if ($rows > 0) {
    $messagerr = "Duplicate Transaction ID.";
    mail("example@test.com", "Duplicate txn_id in the IPN system", $messagerr, "From:        test@example.com" );
    exit(); 
} 

to:

  $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); 
  if ($stmt->rowCount() > 0) {
        $messagerr = "Duplicate Transaction ID.";
        mail("example@test.com", "Duplicate txn_id in the IPN system", $messagerr, "From:        test@example.com" );
        exit(); 
    } 

This checks the number of rows affected by the last SQL statement. Your original code doesn't work because it returns an array. So even if the are no rows, $rows will eaual Array()

  • can u show me a fully answer please. I am still finding it hard to understand what u r saying. I am sorry about it – Angel Aug 08 '13 at 15:01
  • @Angel Did your code look like the above, just with the added changes for using $count;? –  Aug 08 '13 at 15:11
  • @Angel Could you update the code in your original post so I can see what you are doing? –  Aug 08 '13 at 15:23
0

you have 2 extra symbols in your expression

if ($rows) {

would work

It's all you actually need.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345