1--TEST--
2odbc_pconnect(): Make sure closing a persistent connection works
3--EXTENSIONS--
4odbc
5--SKIPIF--
6<?php
7
8include 'skipif.inc';
9
10// The test can affect multiple drivers, but testing it is driver-specific.
11// Since CI runs ODBC tests with SQL Server, just use an SQL Server specific way of testing.
12$conn = odbc_connect($dsn, $user, $pass);
13$result = @odbc_exec($conn, "SELECT @@Version");
14if ($result) {
15    $array = odbc_fetch_array($result);
16    $info = (string) reset($array);
17    if (!str_contains($info, "Microsoft SQL Server")) {
18       echo "skip MS SQL specific test";
19    }
20}
21?>
22--FILE--
23<?php
24
25include 'config.inc';
26
27// set a session specific variable via CONTEXT_INFO, if we get the same connection again, it should be identical
28function set_context_info($conn, $string) {
29	$hexstring = bin2hex($string);
30	return odbc_exec($conn, "SET CONTEXT_INFO 0x$hexstring");
31}
32
33function fetch_context_info($conn) {
34	$stmt = odbc_exec($conn, "SELECT CONTEXT_INFO() AS CONTEXT_INFO");
35	if (!$stmt) {
36		return false;
37	}
38	$res = odbc_fetch_array($stmt);
39	if ($res) {
40		// this is a binary, so we get a bunch of nulls at the end
41		return $res["CONTEXT_INFO"] ? trim($res["CONTEXT_INFO"]) : null;
42	} else {
43		return false;
44	}
45}
46
47// run 1: set expectations
48$conn = odbc_pconnect($dsn, $user, $pass);
49set_context_info($conn, "PHP odbc_pconnect test");
50var_dump(fetch_context_info($conn));
51
52// run 2: reuse same connection (imagine this is another request)
53$conn = odbc_pconnect($dsn, $user, $pass);
54var_dump(fetch_context_info($conn));
55
56// run 3: close it and see if it's the same connection
57odbc_close($conn);
58$conn = odbc_pconnect($dsn, $user, $pass);
59var_dump(fetch_context_info($conn));
60
61?>
62--EXPECT--
63string(22) "PHP odbc_pconnect test"
64string(22) "PHP odbc_pconnect test"
65NULL
66