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 = ?");
$stmt->execute(array('red'));
$count = $stmt->rowCount();
The PDO Statement rowCount() should be what you are looking to do.
EDIT
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
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Then we can use a try/catch block to handle an exception thrown by a SQL statement. Something like this:
try {
$query->execute(...);
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
How to Keep Whitespace Formatting Using PHP/Html
Are PHP Dateinterval Comparable Like Datetime
Simple PHP Echo Code Not Working
File_Get_Contents Throws 400 Bad Request Error PHP
PHP Fatal Error: Call to a Member Function Format() on Boolean
PHP How to Remove Last Part of a Path
Which Is Faster? Constants, Variables or Variable Arrays
Laravel SQLstate[Hy000] [2002] Connection Refused
File_Get_Contents Returns Empty String
How to Find a Reason When Mkdir Fails from PHP
How to Search Array for Multiple Values in PHP
Using Named Parameters with Pdo for Like
Laravel - Decryptexception: 'The MAC Is Invalid'
Sending/Displaying a Base64 Encoded Image
Auto Increment Skipping Numbers
How to Pass an Array of Pdo Parameters Yet Still Specify Their Types