Skip to content

Instantly share code, notes, and snippets.

@crevillo
Last active October 7, 2020 15:51
Show Gist options
  • Save crevillo/bd7b75d4a809a7edeb9c to your computer and use it in GitHub Desktop.
Save crevillo/bd7b75d4a809a7edeb9c to your computer and use it in GitHub Desktop.
<?php
$host="localhost";
$root="root";
$root_password="***************";
$dbh = new PDO("mysql:host=$host", $root, $root_password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::MYSQL_ATTR_FOUND_ROWS, TRUE);
$dbh->query("CREATE DATABASE IF NOT EXISTS `newdb`");
$dbh->query("use newdb");
$dbh->query("CREATE TABLE IF NOT EXISTS tabla (id INT PRIMARY KEY, value INT)");
$statement = $dbh->prepare("INSERT ignore INTO tabla VALUES ( 1, 10 )");
$statement->execute();
$statement = $dbh->prepare("UPDATE tabla SET value = 20 where id = 1");
$statement->execute();
print "rowCount For First update: " . $statement->rowCount() . "\n";
$statement = $dbh->prepare("UPDATE tabla SET value = 20 where id = 1");
$statement->execute();
print "rowCount For First update: " . $statement->rowCount() . "\n";
$dbh->query( "DROP DATABASE newdb" );
/** without MYSQL_ATTR_FOUND_ROWS" **/
$dbh = new PDO("mysql:host=$host", $root, $root_password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->query("CREATE DATABASE IF NOT EXISTS `newdb`");
$dbh->query("use newdb");
$dbh->query("CREATE TABLE IF NOT EXISTS tabla (id INT PRIMARY KEY, value INT)");
$statement = $dbh->prepare("INSERT ignore INTO tabla VALUES ( 1, 10 )");
$statement->execute();
$statement = $dbh->prepare("UPDATE tabla SET value = 20 where id = 1");
$statement->execute();
print "rowCount For First update: " . $statement->rowCount() . "\n";
$statement = $dbh->prepare("UPDATE tabla SET value = 20 where id = 1");
$statement->execute();
print "rowCount For First update: " . $statement->rowCount() . "\n";
$dbh->query( "DROP DATABASE newdb" );
@sparcbr
Copy link

sparcbr commented Oct 7, 2020

rowCount For First update: 1
rowCount For First update: 0
rowCount For First update: 1
rowCount For First update: 0

Is this wrong? I think it should be 1 1 1 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment