Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- =====================
- Valid SQL statements:
- ---------------------
- $sth = $conn->prepare('LOCK TABLE aaa WRITE;'
- .'INSERT INTO aaa (`amount`, `other_column`) VALUES (:amount, :other);'
- .'UNLOCK TABLE aaa;'
- );
- $allOK = $sth->execute(array(':amount' => 42.34, ':other' => 'soq'));
- var_dump('lastInsertId', $conn->lastInsertId(), 'Execute Succeeded?', $allOK);
- What is sent to the mysql server:
- .................................
- 150921 10:54:58 55 Connect test@localhost on testmysql
- 55 Query LOCK TABLE aaa WRITE;INSERT INTO aaa (`amount`, `other_column`) VALUES ('42.340000000000003', 'soq');UNLOCK TABLE aaa
- 54 Query FLUSH LOGS
- Execute Result from PDO:
- .........................
- 'lastInsertId': '0' (length=1)
- 'Execute Succeeded?', boolean true
- Notice - you do not get the 'LastInsertId'.
- Database result:
- ...........................
- 1 record inserted:
- It all worked fine - except you do not know the actual `id` - but you can find it out.
- ============================================
- VERY Invalid SQL statements after the first:
- --------------------------------------------
- $sth = $conn->prepare('LOCK TABLE aaa WRITE;'
- .'INSERT INTO CRAPaaa (`CRAPamount`, `CRAPother_column`) VALUES (:amount, :other);'
- .'UNLOCK TABLE CRAPaaa;'
- );
- $allOK = $sth->execute(array(':amount' => 42.34, ':other' => 'soq'));
- var_dump('lastInsertId', $conn->lastInsertId(), 'Execute Succeeded?', $allOK);
- What is sent to the mysql server:
- .................................
- 63 Connect test@localhost on testmysql
- 63 Query LOCK TABLE aaa WRITE;INSERT INTO CRAPaaa (`CRAPamount`, `CRAPother_column`) VALUES ('42.340000000000003', 'soq');UNLOCK TABLE CRAPaaa
- 62 Query FLUSH LOGS
- Execute Result from PDO:
- .........................
- 'lastInsertId', '0'
- 'Execute Succeeded?', boolean true
- It failed! And there is no error indicated at all!
- Oh dear... ;-/
- // ----------------------------------------------------------------------------
- =====================
- PDO Connection setup:
- ---------------------
- $pdo_connectionstring = 'mysql:host=localhost;dbname=testmysql';
- $pdo_username = 'test';
- $pdo_password = 'test';
- $conn = new PDO($pdo_connectionstring, $pdo_username, $pdo_password);
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement