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	if (!is_null($tmp = @$res->fetch_object($link, $link)))
35		printf("[005] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
36
37	if (!is_null($tmp = @$res->fetch_object($link, $link, $link)))
38		printf("[006] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
39
40	$obj = mysqli_fetch_object($res);
41	if (($obj->ID !== "1") || ($obj->label !== "a") || (get_class($obj) != 'stdClass')) {
42		printf("[007] Object seems wrong. [%d] %s\n", $mysqli->errno, $mysqli->error);
43		var_dump($obj);
44	}
45
46	class mysqli_fetch_object_test {
47
48		public $a = null;
49		public $b = null;
50
51		public function toString() {
52			var_dump($this);
53		}
54	}
55
56	$obj = $res->fetch_object('mysqli_fetch_object_test');
57	if (($obj->ID !== "2") || ($obj->label !== "b") || ($obj->a !== NULL) || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_test')) {
58		printf("[008] Object seems wrong. [%d] %s\n", $mysqli->errno, $mysqli->error);
59		var_dump($obj);
60	}
61
62	class mysqli_fetch_object_construct extends mysqli_fetch_object_test {
63
64		public function __construct($a, $b) {
65			$this->a = $a;
66			$this->b = $b;
67		}
68
69	}
70
71	$obj = $res->fetch_object('mysqli_fetch_object_construct', null);
72
73	if (($obj->ID !== "3") || ($obj->label !== "c") || ($obj->a !== NULL) || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_construct')) {
74		printf("[009] Object seems wrong. [%d] %s\n", $mysqli->errno, $mysqli->error);
75		var_dump($obj);
76	}
77
78	$obj = $res->fetch_object('mysqli_fetch_object_construct', array('a'));
79	if (($obj->ID !== "4") || ($obj->label !== "d") || ($obj->a !== 'a') || ($obj->b !== NULL) || (get_class($obj) != 'mysqli_fetch_object_construct')) {
80		printf("[010] Object seems wrong. [%d] %s\n", $mysqli->errno, $mysqli->error);
81		var_dump($obj);
82	}
83
84	$obj = $res->fetch_object('mysqli_fetch_object_construct', array('a', 'b'));
85	if (($obj->ID !== "5") || ($obj->label !== "e") || ($obj->a !== 'a') || ($obj->b !== 'b') || (get_class($obj) != 'mysqli_fetch_object_construct')) {
86		printf("[011] Object seems wrong. [%d] %s\n", $mysqli->errno, $mysqli->error);
87		var_dump($obj);
88	}
89
90	var_dump($res->fetch_object('mysqli_fetch_object_construct', array('a', 'b', 'c')));
91	var_dump(mysqli_fetch_object($res));
92
93	mysqli_free_result($res);
94
95	if (!$res = mysqli_query($link, "SELECT id AS ID, label FROM test AS TEST")) {
96		printf("[012] [%d] %s\n", $mysqli->errno, $mysqli->error);
97	}
98
99	mysqli_free_result($res);
100
101	var_dump(mysqli_fetch_object($res));
102
103	// Fatal error, script execution will end
104	var_dump($res->fetch_object('this_class_does_not_exist'));
105
106	$mysqli->close();
107	print "done!";
108?>
109--CLEAN--
110<?php
111	require_once("clean_table.inc");
112?>
113--EXPECTF--
114[E_WARNING] mysqli_result::__construct(): invalid object or resource mysql%s
115%s on line %d
116[E_WARNING] mysqli_result::fetch_object(): Couldn't fetch mysqli_result in %s on line %d
117[E_WARNING] mysqli_result::fetch_object() expects parameter 1 to be string, object given in %s on line %d
118[E_RECOVERABLE_ERROR] Argument 2 passed to mysqli_result::fetch_object() must be of the type array, object given in %s on line %d
119[E_WARNING] mysqli_result::fetch_object() expects parameter 1 to be string, object given in %s on line %d
120[E_RECOVERABLE_ERROR] Argument 2 passed to mysqli_result::fetch_object() must be of the type array, object given in %s on line %d
121[E_WARNING] mysqli_result::fetch_object() expects at most 2 parameters, 3 given in %s on line %d
122[E_RECOVERABLE_ERROR] Argument 2 passed to mysqli_result::fetch_object() must be of the type array, null given in %s on line %d
123[E_WARNING] Missing argument 1 for mysqli_fetch_object_construct::__construct() in %s on line %d
124[E_WARNING] Missing argument 2 for mysqli_fetch_object_construct::__construct() in %s on line %d
125[E_NOTICE] Undefined variable: a in %s on line %d
126[E_NOTICE] Undefined variable: b in %s on line %d
127[E_WARNING] Missing argument 2 for mysqli_fetch_object_construct::__construct() in %s on line %d
128[E_NOTICE] Undefined variable: b in %s on line %d
129NULL
130NULL
131[E_WARNING] mysqli_fetch_object(): Couldn't fetch mysqli_result in %s on line %d
132NULL
133
134Fatal error: Class 'this_class_does_not_exist' not found in %s on line %d