File: pdo_mysql_last_insert_id.phpt

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (119 lines) | stat: -rw-r--r-- 5,121 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
--TEST--
MySQL PDO->lastInsertId()
--EXTENSIONS--
pdo_mysql
--SKIPIF--
<?php
require_once __DIR__ . '/inc/mysql_pdo_test.inc';
MySQLPDOTest::skip();
?>
--FILE--
<?php
    require_once __DIR__ . '/inc/mysql_pdo_test.inc';
    $db = MySQLPDOTest::factory();

    try {
        if ('0' !== ($tmp = $db->lastInsertId()))
            printf("[001] No query has been run, lastInsertId() should return '0'/string got '%s'/%s\n",
                var_export($tmp, true), gettype($tmp));

        if ('0' !== ($tmp = $db->lastInsertId('sequence_name')))
            printf("[002] MySQL does not support sequences, expecting '0'/string got '%s'/%s\n",
                var_export($tmp, true), gettype($tmp));

        if ('0' !== ($tmp = $db->lastInsertId()))
            printf("[003] Expecting '0'/string got '%s'/%s", var_export($tmp, true), gettype($tmp));

        $db->exec(sprintf('CREATE TABLE test_pdo_mysql_last_insert_id(id INT, col1 CHAR(10)) ENGINE=%s', PDO_MYSQL_TEST_ENGINE));
        if ('0' !== ($tmp = $db->lastInsertId()))
            printf("[004] Expecting '0'/string got '%s'/%s", var_export($tmp, true), gettype($tmp));

        $stmt = $db->query('SELECT id FROM test_pdo_mysql_last_insert_id LIMIT 1');
        if ('0' !== ($tmp = $db->lastInsertId()))
            printf("[005] Expecting '0'/string got '%s'/%s", var_export($tmp, true), gettype($tmp));

        // no auto increment column
        $db->exec("INSERT INTO test_pdo_mysql_last_insert_id(id, col1) VALUES (100, 'a')");
        if ('0' !== ($tmp = $db->lastInsertId()))
            printf("[006] Expecting '0'/string got '%s'/%s", var_export($tmp, true), gettype($tmp));

        $db->exec('ALTER TABLE test_pdo_mysql_last_insert_id MODIFY id INT AUTO_INCREMENT PRIMARY KEY');
        if ('0' !== ($tmp = $db->lastInsertId()))
            printf("[006] Expecting '0'/string got '%s'/%s", var_export($tmp, true), gettype($tmp));

        // duplicate key
        @$db->exec("INSERT INTO test_pdo_mysql_last_insert_id(id, col1) VALUES (100, 'a')");
        if ('0' !== ($tmp = $db->lastInsertId()))
            printf("[007] Expecting '0'/string got '%s'/%s", var_export($tmp, true), gettype($tmp));

        $db->exec("INSERT INTO test_pdo_mysql_last_insert_id(id, col1) VALUES (101, 'b')");
        if ('101' !== ($tmp = $db->lastInsertId()))
            printf("[008] Expecting '0'/string got '%s'/%s", var_export($tmp, true), gettype($tmp));

        $db->exec('ALTER TABLE test_pdo_mysql_last_insert_id MODIFY col1 CHAR(10) UNIQUE');
        // replace = delete + insert -> new auto increment value
        $db->exec("REPLACE INTO test_pdo_mysql_last_insert_id(col1) VALUES ('b')");
        $next_id = (int)$db->lastInsertId();

        if ($next_id <= 101)
            printf("[009] Expecting at least 102, got %d\n",$next_id);

        $stmt = $db->query('SELECT LAST_INSERT_ID() AS _last_id');
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        $last_id = $row['_last_id'];
        if ($next_id != $last_id) {
            printf("[010] LAST_INSERT_ID() = %d and lastInserId() = %d differ\n",
                $last_id, $next_id);
        }

        $db->exec("INSERT INTO test_pdo_mysql_last_insert_id(col1) VALUES ('c'), ('d'), ('e')");
        $next_id = (int)$db->lastInsertId();
        if ($next_id <= $last_id)
            printf("[011] Expecting at least %d, got %d\n", $last_id + 1, $next_id);

        // warnings are unhandy, lets go for exceptions for a second
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        try {
            $ignore_exception = true;
            $db->exec('LOCK TABLE test_pdo_mysql_last_insert_id WRITE');
            $ignore_exception = false;

            if (MySQLPDOTest::getServerVersion($db) >= 50000) {
                $stmt = $db->query('SELECT @@auto_increment_increment AS inc');
                $row = $stmt->fetch(PDO::FETCH_ASSOC);
                $inc = $row['inc'];
            } else {
                $inc = 1;
            }

            $stmt = $db->query('SELECT LAST_INSERT_ID() AS _last_id');
            $row = $stmt->fetch(PDO::FETCH_ASSOC);
            $last_id = $row['_last_id'];

            $db->exec("INSERT INTO test_pdo_mysql_last_insert_id(col1) VALUES ('z')");
            $next_id = (int)$db->lastInsertId();
            if ($next_id < ($last_id + $inc))
                printf("[012] Expecting at least %d, got %d\n", $last_id + $inc, $next_id);

        } catch (PDOException $e) {
            if (!$ignore_exception)
                printf("[014] %s, [%s} %s\n", $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
        }
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
        @$db->exec('UNLOCK TABLE test_pdo_mysql_last_insert_id');

    } catch (PDOException $e) {
        printf("[001] %s [%s] %s\n",
            $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
    }

    print "done!";
?>
--CLEAN--
<?php
require_once __DIR__ . '/inc/mysql_pdo_test.inc';
$db = MySQLPDOTest::factory();
$db->exec('DROP TABLE IF EXISTS test_pdo_mysql_last_insert_id');
?>
--EXPECT--
done!