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	$obj = $res->fetch_object('mysqli_fetch_object_construct', array('a'));
93	if (($obj->ID !== "4") || ($obj->label !== "d") || ($obj->a !== 'a') || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_construct')) {
94		printf("[010] Object seems wrong. [%d] %s\n", $mysqli->errno, $mysqli->error);
95		var_dump($obj);
96	}
97
98	$obj = $res->fetch_object('mysqli_fetch_object_construct', array('a', 'b'));
99	if (($obj->ID !== "5") || ($obj->label !== "e") || ($obj->a !== 'a') || ($obj->b !== 'b') || (get_class($obj) != 'mysqli_fetch_object_construct')) {
100		printf("[011] Object seems wrong. [%d] %s\n", $mysqli->errno, $mysqli->error);
101		var_dump($obj);
102	}
103
104	var_dump($res->fetch_object('mysqli_fetch_object_construct', array('a', 'b', 'c')));
105	var_dump(mysqli_fetch_object($res));
106
107	mysqli_free_result($res);
108
109	if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST")) {
110		printf("[012] [%d] %s\n", $mysqli->errno, $mysqli->error);
111	}
112
113	mysqli_free_result($res);
114
115	var_dump(mysqli_fetch_object($res));
116
117	// Fatal error, script execution will end
118	var_dump($res->fetch_object('this_class_does_not_exist'));
119
120	$mysqli->close();
121	print "done!";
122?>
123--CLEAN--
124<?php
125	require_once("clean_table.inc");
126?>
127--EXPECTF--
128[E_WARNING] mysqli_result::__construct(): invalid object or resource mysql%s
129%s on line %d
130[E_WARNING] mysqli_result::fetch_object(): Couldn't fetch mysqli_result in %s on line %d
131[E_WARNING] mysqli_result::fetch_object() expects parameter 1 to be string, object given in %s on line %d
132[0] Argument 2 passed to mysqli_result::fetch_object() must be of the type array, object given in %s on line %d
133[0] Argument 2 passed to mysqli_result::fetch_object() must be of the type array, object given in %s on line %d
134[0] Argument 2 passed to mysqli_result::fetch_object() must be of the type array, null given in %s on line %d
135[E_WARNING] Missing argument 2 for mysqli_fetch_object_construct::__construct() in %s on line %d
136[E_NOTICE] Undefined variable: b in %s on line %d
137NULL
138NULL
139[E_WARNING] mysqli_fetch_object(): Couldn't fetch mysqli_result in %s on line %d
140NULL
141
142Fatal error: Class 'this_class_does_not_exist' not found in %s on line %d
143