1--TEST--
2mysqli_pconnect() - reusing/caching persistent connections - TODO
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7die("skip TODO - we need to add a user level way to check if CHANGE_USER gets called by pconnect");
8
9require_once('skipifconnectfailure.inc');
10?>
11--INI--
12mysqli.allow_persistent=1
13mysqli.max_persistent=2
14mysqli.max_links=2
15--FILE--
16<?php
17    require_once("connect.inc");
18
19    $host = 'p:' . $host;
20    if (!$link1 = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
21        printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s, [%d] %s\n",
22            $host, $user, $db, $port, $socket, mysqli_connect_errno(), mysqli_connect_error());
23    }
24    if (!mysqli_query($link1, 'SET @pcondisabled = "Connection 1"'))
25        printf("[002] Cannot set user variable to check if we got the same persistent connection, [%d] %s\n",
26            mysqli_errno($link1), mysqli_error($link1));
27
28    if (!$link2 = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
29        printf("[003] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s, [%d] %s\n",
30            $host, $user, $db, $port, $socket, mysqli_connect_errno(), mysqli_connect_error());
31    }
32
33    if (!$res = mysqli_query($link1, 'SELECT @pcondisabled AS _test'))
34        printf("[004] [%d] %s\n", mysqli_errno($link2), mysqli_error($link2));
35
36    $row = mysqli_fetch_assoc($res);
37    printf("Connection 1 - SELECT @pcondisabled -> '%s'\n", $row['_test']);
38    mysqli_free_result($res);
39
40    if (!$res = mysqli_query($link2, 'SELECT @pcondisabled AS _test'))
41        printf("[005] [%d] %s\n", mysqli_errno($link2), mysqli_error($link2));
42
43    $row = mysqli_fetch_assoc($res);
44    printf("Connection 2 (no reuse) - SELECT @pcondisabled -> '%s'\n", $row['_test']);
45    $thread_id = mysqli_thread_id($link2);
46    printf("Connection 2 (no reuse) - Thread ID -> '%s'\n", $thread_id);
47    mysqli_free_result($res);
48
49    if (!mysqli_query($link2, 'SET @pcondisabled = "Connection 2"'))
50        printf("[006] Cannot set user variable to check if we got the same persistent connection, [%d] %s\n",
51            mysqli_errno($link2), mysqli_error($link2));
52
53    if (!$res = mysqli_query($link2, 'SELECT @pcondisabled AS _test'))
54        printf("[007] [%d] %s\n", mysqli_errno($link2), mysqli_error($link2));
55
56    $row = mysqli_fetch_assoc($res);
57    printf("Connection 2 - SELECT @pcondisabled -> '%s'\n", $row['_test']);
58    mysqli_free_result($res);
59
60    mysqli_close($link2);
61
62    /* reuse of existing persistent connection expected! */
63    if (!$link2 = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
64        printf("[008] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s, [%d] %s\n",
65            $host, $user, $db, $port, $socket, mysqli_connect_errno(), mysqli_connect_error());
66    }
67
68    if (!$res = mysqli_query($link2, 'SELECT @pcondisabled AS _test'))
69        printf("[009] [%d] %s\n", mysqli_errno($link2), mysqli_error($link2));
70
71    $row = mysqli_fetch_assoc($res);
72    printf("Connection 2 (reuse) - SELECT @pcondisabled -> '%s'\n", $row['_test']);
73    $thread_id_reuse = mysqli_thread_id($link2);
74    printf("Connection 2 (reuse) - Thread ID -> '%s'\n", $thread_id_reuse);
75    mysqli_free_result($res);
76
77    if ($thread_id != $thread_id_reuse)
78        printf("[010] Seems as if we have got a new connection, connections should have been cached and reused!\n");
79
80    mysqli_close($link1);
81    mysqli_close($link2);
82    print "done!";
83?>
84--EXPECTF--
85Connection 1 - SELECT @pcondisabled -> 'Connection 1'
86Connection 2 (no reuse) - SELECT @pcondisabled -> ''
87Connection 2 (no reuse) - Thread ID -> '%d'
88Connection 2 - SELECT @pcondisabled -> 'Connection 2'
89Connection 2 (reuse) - SELECT @pcondisabled -> 'Connection 2'
90Connection 2 (reuse) - Thread ID -> '%d'
91done!
92