1--TEST--
2mysqli_fetch_object()
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
7require_once('skipifconnectfailure.inc');
8?>
9--FILE--
10<?php
11	require_once("connect.inc");
12	set_error_handler('handle_catchable_fatal');
13
14	$tmp    = NULL;
15	$link   = NULL;
16
17	$mysqli = new mysqli();
18	$res = @new mysqli_result($mysqli);
19	if (!is_null($tmp = @$res->fetch_object()))
20		printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
21
22	require('table.inc');
23	if (!$mysqli = new my_mysqli($host, $user, $passwd, $db, $port, $socket))
24		printf("[002] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
25			$host, $user, $db, $port, $socket);
26
27	if (!$res = $mysqli->query("SELECT id AS ID, label FROM test AS TEST ORDER BY id LIMIT 5")) {
28		printf("[003] [%d] %s\n", $mysqli->errno, $mysqli->error);
29	}
30
31	if (!is_null($tmp = @$res->fetch_object($link)))
32		printf("[004] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
33
34	try {
35		if (!is_null($tmp = @$res->fetch_object($link, $link)))
36			printf("[005] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
37	} catch (Error $e) {
38		handle_catchable_fatal($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
39	}
40
41
42	try {
43		if (!is_null($tmp = @$res->fetch_object($link, $link, $link)))
44			printf("[006] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
45	} catch (Error $e) {
46		handle_catchable_fatal($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
47	}
48
49	$obj = mysqli_fetch_object($res);
50	if (($obj->ID !== "1") || ($obj->label !== "a") || (get_class($obj) != 'stdClass')) {
51		printf("[007] Object seems wrong. [%d] %s\n", $mysqli->errno, $mysqli->error);
52		var_dump($obj);
53	}
54
55	class mysqli_fetch_object_test {
56
57		public $a = null;
58		public $b = null;
59
60		public function toString() {
61			var_dump($this);
62		}
63	}
64
65	$obj = $res->fetch_object('mysqli_fetch_object_test');
66	if (($obj->ID !== "2") || ($obj->label !== "b") || ($obj->a !== NULL) || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_test')) {
67		printf("[008] Object seems wrong. [%d] %s\n", $mysqli->errno, $mysqli->error);
68		var_dump($obj);
69	}
70
71	class mysqli_fetch_object_construct extends mysqli_fetch_object_test {
72
73		public function __construct($a, $b) {
74			$this->a = $a;
75			$this->b = $b;
76		}
77
78	}
79
80	try {
81		$obj = $res->fetch_object('mysqli_fetch_object_construct', null);
82
83		if (($obj->ID !== "3") || ($obj->label !== "c") || ($obj->a !== NULL) || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_construct')) {
84			printf("[009] Object seems wrong. [%d] %s\n", $mysqli->errno, $mysqli->error);
85			var_dump($obj);
86		}
87	} catch (Error $e) {
88		handle_catchable_fatal($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
89		mysqli_fetch_object($res);
90	}
91
92	try {
93		$obj = $res->fetch_object('mysqli_fetch_object_construct', array('a'));
94		if (($obj->ID !== "4") || ($obj->label !== "d") || ($obj->a !== 'a') || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_construct')) {
95			printf("[010] Object seems wrong. [%d] %s\n", $mysqli->errno, $mysqli->error);
96			var_dump($obj);
97		}
98	} catch (Throwable $e) {
99		echo "Exception: " . $e->getMessage() . "\n";
100	}
101
102	$obj = $res->fetch_object('mysqli_fetch_object_construct', array('a', 'b'));
103	if (($obj->ID !== "5") || ($obj->label !== "e") || ($obj->a !== 'a') || ($obj->b !== 'b') || (get_class($obj) != 'mysqli_fetch_object_construct')) {
104		printf("[011] Object seems wrong. [%d] %s\n", $mysqli->errno, $mysqli->error);
105		var_dump($obj);
106	}
107
108	var_dump($res->fetch_object('mysqli_fetch_object_construct', array('a', 'b', 'c')));
109	var_dump(mysqli_fetch_object($res));
110
111	mysqli_free_result($res);
112
113	if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST")) {
114		printf("[012] [%d] %s\n", $mysqli->errno, $mysqli->error);
115	}
116
117	mysqli_free_result($res);
118
119	var_dump(mysqli_fetch_object($res));
120
121	// Fatal error, script execution will end
122	var_dump($res->fetch_object('this_class_does_not_exist'));
123
124	$mysqli->close();
125	print "done!";
126?>
127--CLEAN--
128<?php
129	require_once("clean_table.inc");
130?>
131--EXPECTF--
132[E_WARNING] mysqli_result::__construct(): invalid object or resource mysql%s
133%s on line %d
134[E_WARNING] mysqli_result::fetch_object(): Couldn't fetch mysqli_result in %s on line %d
135[E_WARNING] mysqli_result::fetch_object() expects parameter 1 to be string, object given in %s on line %d
136[0] Argument 2 passed to mysqli_result::fetch_object() must be of the type array, object given in %s on line %d
137[0] Argument 2 passed to mysqli_result::fetch_object() must be of the type array, object given in %s on line %d
138[0] Argument 2 passed to mysqli_result::fetch_object() must be of the type array, null given in %s on line %d
139Exception: Too few arguments to function mysqli_fetch_object_construct::__construct(), 1 passed and exactly 2 expected
140NULL
141NULL
142[E_WARNING] mysqli_fetch_object(): Couldn't fetch mysqli_result in %s on line %d
143NULL
144
145Fatal error: Class 'this_class_does_not_exist' not found in %s on line %d
146