iswinky
iswinky

Reputation: 1969

Count execute()s successfully performed PDO php

I currently use a function within a class to insert data into a database, and if each row is successfully insert (from csv file) a message is logged (logMessage function), to show which row was or was not successful. However I would like the count of the successful executions that have been imported into the database. And I'm having a bit of trouble

public function insertData($data, $name, $quantity, $date){

        try {
            $sql = "INSERT INTO `test`(data, name, quantity, date) VALUES(:data, :name, :quantity, :date)";
            $stmt = $this->conn->prepare($sql);
            $stmt->bindParam(":data", $data);
            $stmt->bindParam(":name", $name);
            $stmt->bindParam(":quantity", $quantity);
            $stmt->bindParam(":date", $date);

            if($stmt->execute()){
                FileProcessor::logMessage("Data imported: <b>$data</b>");
            } else {
                FileProcessor::logMessage("Not Imported <b>$data</b>");
            }
        } catch (PDOException $e){
            FileProcessor::logMessage("Error: " . $e->getMessage());
        }
    }

Upvotes: 0

Views: 71

Answers (2)

Aris
Aris

Reputation: 5057

Return the rowCount() in each function call and sum up all the return values.

For more, you must show how you call the function, not the function itself. I suppose you call it from a loop.

Upvotes: 0

Rijndael
Rijndael

Reputation: 3753

You are looking for rowCount(), see http://www.php.net/manual/en/pdostatement.rowcount.php

Upvotes: 2

Related Questions