Pdo Were Rows Affected During Execute Statement

PDO were rows affected during execute statement

Try $q->rowCount(). Prepared statements will return the number of affected rows via that method.

PDO exec returns 0 instead of rows affected

Pdo exec doesn't support prepared statements. So, whatever behavior of exec() method has nothing to do with PDO but belongs to whatever framework you are using.

How to return false if 0 rows affected with PDO?

Try this

return $stmt->rowCount() > 0;

PHP/PDO: use simple prepared statement with query return/affected rows?

It should be the same as any other statement:

$stmt = $dbh->prepare("DELETE FROM fruit WHERE colour = ?");
$count = $stmt->rowCount();

The PDO Statement rowCount() should be what you are looking to do.


Fixed by adding the ->rowCount() which will return the row count. ->execute in a statement will return a bool, true or false whether the query errored out or not. Of course all of this information is readily available at the PDO Statement Manual

Can't get number of affected rows with PDO

SELECT * FROM WHERE uye_ka = :kullanici_adi

Select from Where actually? You need to specify a table name

SELECT * FROM  myTable WHERE uye_ka = :kullanici_adi

Same for your second query.

PHP PDO MySQL Correct way to check if an update query succeeded when no rows are affected

The execute() method will either throw an exception or return FALSE (depending on the error mode you have set for the database connection) when the execution of a SQL statement fails.

If we set the error mode to throw an exception, prior to executing a statement, (usually immediately after establishing a database connection), like this


Then we can use a try/catch block to handle an exception thrown by a SQL statement. Something like this:

try {
echo 'Update succeeded';

} catch(PDOException $e) {
echo 'Update failed!';
echo 'Error: ' . $e->getMessage();

Information about error mode settings and handling is available in the documentation here: http://php.net/manual/en/pdo.error-handling.php

Or, if PDO isn't set to throw an exception, we can use a simple if test. (The execute() method will return FALSE if the the statement fails.)

if ($query->execute(...)) {
echo 'Update succeeded';

} else {
echo 'Update failed!';


For more precise control with the different types of failure, we can use the errorCode() method to retrieve the SQLSTATE associated with the last operation on the statement handle, and we can perform conditional tests on the returned value. http://php.net/manual/en/pdostatement.errorcode.php

Related Topics

Leave a reply