xref: /PHP-8.1/ext/mysqli/mysqli_nonapi.c (revision 5b603709)
1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) The PHP Group                                          |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.01 of the PHP license,      |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | https://www.php.net/license/3_01.txt                                 |
9   | If you did not receive a copy of the PHP license and are unable to   |
10   | obtain it through the world-wide-web, please send a note to          |
11   | license@php.net so we can mail you a copy immediately.               |
12   +----------------------------------------------------------------------+
13   | Authors: Georg Richter <georg@php.net>                               |
14   |          Andrey Hristov <andrey@php.net>                             |
15   |          Ulf Wendel <uw@php.net>                                     |
16   +----------------------------------------------------------------------+
17 */
18 
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #include <signal.h>
24 
25 #include "php.h"
26 #include "php_ini.h"
27 #include "ext/standard/info.h"
28 #include "zend_smart_str.h"
29 #include "php_mysqli_structs.h"
30 #include "mysqli_priv.h"
31 #define ERROR_ARG_POS(arg_num) (getThis() ? (arg_num-1) : (arg_num))
32 
33 #define SAFE_STR(a) ((a)?a:"")
34 
35 /* {{{ php_mysqli_set_error */
php_mysqli_set_error(zend_long mysql_errno,char * mysql_err)36 static void php_mysqli_set_error(zend_long mysql_errno, char *mysql_err)
37 {
38 	MyG(error_no) = mysql_errno;
39 	if (MyG(error_msg)) {
40 		efree(MyG(error_msg));
41 	}
42 	if(mysql_err && *mysql_err) {
43 		MyG(error_msg) = estrdup(mysql_err);
44 	} else {
45 		MyG(error_msg) = NULL;
46 	}
47 }
48 /* }}} */
49 
mysqli_common_connect(INTERNAL_FUNCTION_PARAMETERS,bool is_real_connect,bool in_ctor)50 void mysqli_common_connect(INTERNAL_FUNCTION_PARAMETERS, bool is_real_connect, bool in_ctor) /* {{{ */
51 {
52 	MY_MYSQL			*mysql = NULL;
53 	MYSQLI_RESOURCE		*mysqli_resource = NULL;
54 	zval				*object = getThis();
55 	char				*hostname = NULL, *username=NULL, *passwd=NULL, *dbname=NULL, *socket=NULL,
56 						*ssl_key = NULL, *ssl_cert = NULL, *ssl_ca = NULL, *ssl_capath = NULL,
57 						*ssl_cipher = NULL;
58 	size_t				hostname_len = 0, username_len = 0, passwd_len = 0, dbname_len = 0, socket_len = 0;
59 	bool			persistent = false, ssl = false;
60 	zend_long			port = 0, flags = 0;
61 	bool           port_is_null = 1;
62 	zend_string			*hash_key = NULL;
63 	bool			new_connection = false;
64 	zend_resource		*le;
65 	mysqli_plist_entry *plist = NULL;
66 	bool			self_alloced = 0;
67 
68 
69 #if !defined(MYSQL_USE_MYSQLND)
70 	if ((MYSQL_VERSION_ID / 100) != (mysql_get_client_version() / 100)) {
71 		php_error_docref(NULL, E_WARNING,
72 						"Headers and client library minor version mismatch. Headers:%d Library:%ld",
73 						MYSQL_VERSION_ID, mysql_get_client_version());
74 	}
75 #endif
76 
77 	if (getThis() && !ZEND_NUM_ARGS() && in_ctor) {
78 		php_mysqli_init(INTERNAL_FUNCTION_PARAM_PASSTHRU, in_ctor);
79 		return;
80 	}
81 	hostname = username = dbname = passwd = socket = NULL;
82 
83 	if (!is_real_connect) {
84 		if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!s!s!s!l!s!", &hostname, &hostname_len, &username, &username_len,
85 				&passwd, &passwd_len, &dbname, &dbname_len, &port, &port_is_null, &socket, &socket_len) == FAILURE) {
86 			RETURN_THROWS();
87 		}
88 
89 		if (object) {
90 			ZEND_ASSERT(instanceof_function(Z_OBJCE_P(object), mysqli_link_class_entry));
91 			mysqli_resource = (Z_MYSQLI_P(object))->ptr;
92 			if (mysqli_resource && mysqli_resource->ptr) {
93 				mysql = (MY_MYSQL*) mysqli_resource->ptr;
94 			}
95 		}
96 		if (!mysql) {
97 			mysql = (MY_MYSQL *) ecalloc(1, sizeof(MY_MYSQL));
98 			self_alloced = 1;
99 		}
100 		flags |= CLIENT_MULTI_RESULTS; /* needed for mysql_multi_query() */
101 	} else {
102 		/* We have flags too */
103 		if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|s!s!s!s!l!s!l", &object, mysqli_link_class_entry,
104 				&hostname, &hostname_len, &username, &username_len, &passwd, &passwd_len, &dbname, &dbname_len, &port, &port_is_null, &socket, &socket_len, &flags) == FAILURE) {
105 			RETURN_THROWS();
106 		}
107 
108 		mysqli_resource = (Z_MYSQLI_P(object))->ptr;
109 		MYSQLI_FETCH_RESOURCE_CONN(mysql, object, MYSQLI_STATUS_INITIALIZED);
110 
111 		/* set some required options */
112 		flags |= CLIENT_MULTI_RESULTS; /* needed for mysql_multi_query() */
113 		/* remove some insecure options */
114 		flags &= ~CLIENT_MULTI_STATEMENTS;   /* don't allow multi_queries via connect parameter */
115 #ifndef MYSQLI_USE_MYSQLND
116 		if (PG(open_basedir) && PG(open_basedir)[0] != '\0') {
117 			flags &= ~CLIENT_LOCAL_FILES;
118 		}
119 #endif
120 	}
121 
122 	if (!socket_len || !socket) {
123 		socket = MyG(default_socket);
124 	}
125 	if (port_is_null || !port) {
126 		port = MyG(default_port);
127 	}
128 	if (!passwd) {
129 		passwd = MyG(default_pw);
130 		passwd_len = strlen(SAFE_STR(passwd));
131 	}
132 	if (!username){
133 		username = MyG(default_user);
134 	}
135 	if (!hostname || !hostname_len) {
136 		hostname = MyG(default_host);
137 	}
138 
139 	if (mysql->mysql && mysqli_resource &&
140 		(mysqli_resource->status > MYSQLI_STATUS_INITIALIZED))
141 	{
142 		/* already connected, we should close the connection */
143 		php_mysqli_close(mysql, MYSQLI_CLOSE_IMPLICIT, mysqli_resource->status);
144 	}
145 
146 	if (strlen(SAFE_STR(hostname)) > 2 && !strncasecmp(hostname, "p:", 2)) {
147 		hostname += 2;
148 		if (!MyG(allow_persistent)) {
149 			php_error_docref(NULL, E_WARNING, "Persistent connections are disabled. Downgrading to normal");
150 		} else {
151 			mysql->persistent = persistent = true;
152 
153 			hash_key = strpprintf(0, "mysqli_%s_%s" ZEND_LONG_FMT "%s%s%s", SAFE_STR(hostname), SAFE_STR(socket),
154 								port, SAFE_STR(username), SAFE_STR(dbname),
155 								SAFE_STR(passwd));
156 
157 			mysql->hash_key = hash_key;
158 
159 			/* check if we can reuse existing connection ... */
160 			if ((le = zend_hash_find_ptr(&EG(persistent_list), hash_key)) != NULL) {
161 				if (le->type == php_le_pmysqli()) {
162 					plist = (mysqli_plist_entry *) le->ptr;
163 
164 					do {
165 						if (zend_ptr_stack_num_elements(&plist->free_links)) {
166 							/* If we have an initialized (but unconnected) mysql resource,
167 							 * close it before we reuse an existing persistent resource. */
168 							if (mysql->mysql) {
169 								mysqli_close(mysql->mysql, MYSQLI_CLOSE_IMPLICIT);
170 							}
171 
172 							mysql->mysql = zend_ptr_stack_pop(&plist->free_links);
173 
174 							MyG(num_inactive_persistent)--;
175 							/* reset variables */
176 
177 #ifndef MYSQLI_NO_CHANGE_USER_ON_PCONNECT
178 							if (!mysqli_change_user_silent(mysql->mysql, username, passwd, dbname, passwd_len)) {
179 #else
180 							if (!mysql_ping(mysql->mysql)) {
181 #endif
182 #ifdef MYSQLI_USE_MYSQLND
183 								mysqlnd_restart_psession(mysql->mysql);
184 #endif
185 								MyG(num_active_persistent)++;
186 
187 								/* clear error */
188 								php_mysqli_set_error(mysql_errno(mysql->mysql), (char *) mysql_error(mysql->mysql));
189 
190 								goto end;
191 							} else {
192 #ifdef MYSQLI_USE_MYSQLND
193 								if (mysql->mysql->data->vio->data->ssl) {
194 									/* copy over pre-existing ssl settings so we can reuse them when reconnecting */
195 									ssl = true;
196 
197 									ssl_key = mysql->mysql->data->vio->data->options.ssl_key ? estrdup(mysql->mysql->data->vio->data->options.ssl_key) : NULL;
198 									ssl_cert = mysql->mysql->data->vio->data->options.ssl_cert ? estrdup(mysql->mysql->data->vio->data->options.ssl_cert) : NULL;
199 									ssl_ca = mysql->mysql->data->vio->data->options.ssl_ca ? estrdup(mysql->mysql->data->vio->data->options.ssl_ca) : NULL;
200 									ssl_capath = mysql->mysql->data->vio->data->options.ssl_capath ? estrdup(mysql->mysql->data->vio->data->options.ssl_capath) : NULL;
201 									ssl_cipher = mysql->mysql->data->vio->data->options.ssl_cipher ? estrdup(mysql->mysql->data->vio->data->options.ssl_cipher) : NULL;
202 								}
203 #else
204 								if (mysql->mysql->options.ssl_key
205 										|| mysql->mysql->options.ssl_cert
206 										|| mysql->mysql->options.ssl_ca
207 										|| mysql->mysql->options.ssl_capath
208 										|| mysql->mysql->options.ssl_cipher) {
209 									/* copy over pre-existing ssl settings so we can reuse them when reconnecting */
210 									ssl = true;
211 
212 									ssl_key = mysql->mysql->options.ssl_key ? estrdup(mysql->mysql->options.ssl_key) : NULL;
213 									ssl_cert = mysql->mysql->options.ssl_cert ? estrdup(mysql->mysql->options.ssl_cert) : NULL;
214 									ssl_ca = mysql->mysql->options.ssl_ca ? estrdup(mysql->mysql->options.ssl_ca) : NULL;
215 									ssl_capath = mysql->mysql->options.ssl_capath ? estrdup(mysql->mysql->options.ssl_capath) : NULL;
216 									ssl_cipher = mysql->mysql->options.ssl_cipher ? estrdup(mysql->mysql->options.ssl_cipher) : NULL;
217 								}
218 #endif
219 								mysqli_close(mysql->mysql, MYSQLI_CLOSE_IMPLICIT);
220 								mysql->mysql = NULL;
221 							}
222 						}
223 					} while (0);
224 				}
225 			} else {
226 				plist = calloc(1, sizeof(mysqli_plist_entry));
227 
228 				zend_ptr_stack_init_ex(&plist->free_links, 1);
229 				zend_register_persistent_resource(ZSTR_VAL(hash_key), ZSTR_LEN(hash_key), plist, php_le_pmysqli());
230 			}
231 		}
232 	}
233 	if (MyG(max_links) != -1 && MyG(num_links) >= MyG(max_links)) {
234 		php_error_docref(NULL, E_WARNING, "Too many open links (" ZEND_LONG_FMT ")", MyG(num_links));
235 		goto err;
236 	}
237 
238 	if (persistent && MyG(max_persistent) != -1 &&
239 		(MyG(num_active_persistent) + MyG(num_inactive_persistent))>= MyG(max_persistent))
240 	{
241 		php_error_docref(NULL, E_WARNING, "Too many open persistent links (" ZEND_LONG_FMT ")",
242 								MyG(num_active_persistent) + MyG(num_inactive_persistent));
243 		goto err;
244 	}
245 	if (!mysql->mysql) {
246 #ifndef MYSQLI_USE_MYSQLND
247 		if (!(mysql->mysql = mysql_init(NULL))) {
248 #else
249 		if (!(mysql->mysql = mysqlnd_init(MYSQLND_CLIENT_NO_FLAG, persistent))) {
250 #endif
251 			goto err;
252 		}
253 		new_connection = true;
254 	}
255 
256 #ifndef MYSQLI_USE_MYSQLND
257 	/* BC for prior to bug fix #53425 */
258 	flags |= CLIENT_MULTI_RESULTS;
259 
260 	if (ssl) {
261 		/* if we're here, this means previous conn was ssl, repopulate settings */
262 		mysql_ssl_set(mysql->mysql, ssl_key, ssl_cert, ssl_ca, ssl_capath, ssl_cipher);
263 
264 		if (ssl_key) {
265 		    efree(ssl_key);
266 		}
267 
268 		if (ssl_cert) {
269 		    efree(ssl_cert);
270 		}
271 
272 		if (ssl_ca) {
273 		    efree(ssl_ca);
274 		}
275 
276 		if (ssl_capath) {
277 		    efree(ssl_capath);
278 		}
279 
280 		if (ssl_cipher) {
281 		    efree(ssl_cipher);
282 		}
283 	}
284 	if (mysql_real_connect(mysql->mysql, hostname, username, passwd, dbname, port, socket, flags) == NULL)
285 #else
286 	if (ssl) {
287 		/* if we're here, this means previous conn was ssl, repopulate settings */
288 		mysql_ssl_set(mysql->mysql, ssl_key, ssl_cert, ssl_ca, ssl_capath, ssl_cipher);
289 
290 		if (ssl_key) {
291 		    efree(ssl_key);
292 		}
293 
294 		if (ssl_cert) {
295 		    efree(ssl_cert);
296 		}
297 
298 		if (ssl_ca) {
299 		    efree(ssl_ca);
300 		}
301 
302 		if (ssl_capath) {
303 		    efree(ssl_capath);
304 		}
305 
306 		if (ssl_cipher) {
307 		    efree(ssl_cipher);
308 		}
309 	}
310 	if (mysqlnd_connect(mysql->mysql, hostname, username, passwd, passwd_len, dbname, dbname_len,
311 						port, socket, flags, MYSQLND_CLIENT_NO_FLAG) == NULL)
312 #endif
313 	{
314 		/* Save error messages - for mysqli_connect_error() & mysqli_connect_errno() */
315 		php_mysqli_set_error(mysql_errno(mysql->mysql), (char *) mysql_error(mysql->mysql));
316 		php_mysqli_throw_sql_exception((char *)mysql_sqlstate(mysql->mysql), mysql_errno(mysql->mysql),
317 										"%s", mysql_error(mysql->mysql));
318 		if (!is_real_connect) {
319 			/* free mysql structure */
320 			mysqli_close(mysql->mysql, MYSQLI_CLOSE_DISCONNECTED);
321 			mysql->mysql = NULL;
322 		}
323 		goto err;
324 	}
325 
326 	/* clear error */
327 	php_mysqli_set_error(mysql_errno(mysql->mysql), (char *) mysql_error(mysql->mysql));
328 
329 #ifndef MYSQLI_USE_MYSQLND
330 	char reconnect = MyG(reconnect);
331 	mysql_options(mysql->mysql, MYSQL_OPT_RECONNECT, (char *)&reconnect);
332 #endif
333 	unsigned int allow_local_infile = MyG(allow_local_infile);
334 	mysql_options(mysql->mysql, MYSQL_OPT_LOCAL_INFILE, (char *)&allow_local_infile);
335 
336 #if (MYSQL_VERSION_ID >= 80021 && !defined(MARIADB_BASE_VERSION)) || defined(MYSQLI_USE_MYSQLND)
337 	if (MyG(local_infile_directory) && !php_check_open_basedir(MyG(local_infile_directory))) {
338 		mysql_options(mysql->mysql, MYSQL_OPT_LOAD_DATA_LOCAL_DIR, MyG(local_infile_directory));
339 	}
340 #endif
341 
342 end:
343 	if (!mysqli_resource) {
344 		mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
345 		mysqli_resource->ptr = (void *)mysql;
346 	}
347 	mysqli_resource->status = MYSQLI_STATUS_VALID;
348 
349 	/* store persistent connection */
350 	if (persistent && (new_connection || is_real_connect)) {
351 		MyG(num_active_persistent)++;
352 	}
353 
354 	MyG(num_links)++;
355 
356 	mysql->multi_query = 0;
357 
358 	if (object) {
359 		ZEND_ASSERT(instanceof_function(Z_OBJCE_P(object), mysqli_link_class_entry));
360 		(Z_MYSQLI_P(object))->ptr = mysqli_resource;
361 		RETURN_TRUE;
362 	} else {
363 		MYSQLI_RETVAL_RESOURCE(mysqli_resource, mysqli_link_class_entry);
364 		return;
365 	}
366 
367 err:
368 	if (mysql->hash_key) {
369 		zend_string_release_ex(mysql->hash_key, 0);
370 		mysql->hash_key = NULL;
371 		mysql->persistent = false;
372 	}
373 	if (!is_real_connect && self_alloced) {
374 		efree(mysql);
375 	}
376 	RETVAL_FALSE;
377 } /* }}} */
378 
379 /* {{{ Open a connection to a mysql server */
380 PHP_FUNCTION(mysqli_connect)
381 {
382 	mysqli_common_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, false, false);
383 }
384 /* }}} */
385 
386 PHP_METHOD(mysqli, __construct)
387 {
388 	mysqli_common_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, false, true);
389 }
390 
391 /* {{{ Initialize mysqli and return a resource for use with mysql_real_connect */
392 PHP_METHOD(mysqli, init)
393 {
394 	php_mysqli_init(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
395 }
396 /* }}} */
397 
398 /* {{{ Returns the numerical value of the error message from last connect command */
399 PHP_FUNCTION(mysqli_connect_errno)
400 {
401 	if (zend_parse_parameters_none() == FAILURE) {
402 		RETURN_THROWS();
403 	}
404 
405 	RETURN_LONG(MyG(error_no));
406 }
407 /* }}} */
408 
409 /* {{{ Returns the text of the error message from previous MySQL operation */
410 PHP_FUNCTION(mysqli_connect_error)
411 {
412 	if (zend_parse_parameters_none() == FAILURE) {
413 		RETURN_THROWS();
414 	}
415 
416 	if (MyG(error_msg)) {
417 		RETURN_STRING(MyG(error_msg));
418 	} else {
419 		RETURN_NULL();
420 	}
421 }
422 /* }}} */
423 
424 /* {{{ Fetch a result row as an associative array, a numeric array, or both */
425 PHP_FUNCTION(mysqli_fetch_array)
426 {
427 	php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 0);
428 }
429 /* }}} */
430 
431 /* {{{ Fetch a result row as an associative array */
432 PHP_FUNCTION(mysqli_fetch_assoc)
433 {
434 	php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQLI_ASSOC, 0);
435 }
436 /* }}} */
437 
438 /* {{{ Fetch a column from the result row  */
439 PHP_FUNCTION(mysqli_fetch_column)
440 {
441 	MYSQL_RES		*result;
442 	zval			*mysql_result;
443 	zval 			row_array;
444 	zend_long		col_no = 0;
445 
446 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|l", &mysql_result, mysqli_result_class_entry, &col_no) == FAILURE) {
447 		RETURN_THROWS();
448 	}
449 	MYSQLI_FETCH_RESOURCE(result, MYSQL_RES*, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
450 
451 	if (col_no < 0) {
452 		zend_argument_value_error(ERROR_ARG_POS(2), "must be greater than or equal to 0");
453 		RETURN_THROWS();
454 	}
455 	if (col_no >= mysql_num_fields(result)) {
456 		zend_argument_value_error(ERROR_ARG_POS(2), "must be less than the number of fields for this result set");
457 		RETURN_THROWS();
458 	}
459 
460 	php_mysqli_fetch_into_hash_aux(&row_array, result, MYSQLI_NUM);
461 	if (Z_TYPE(row_array) != IS_ARRAY) {
462 		zval_ptr_dtor_nogc(&row_array);
463 		RETURN_FALSE;
464 	}
465 
466 	ZVAL_COPY(return_value, zend_hash_index_find(Z_ARR(row_array), col_no));
467 	zval_ptr_dtor_nogc(&row_array);
468 }
469 /* }}} */
470 
471 /* {{{ Fetches all result rows as an associative array, a numeric array, or both */
472 PHP_FUNCTION(mysqli_fetch_all)
473 {
474 	MYSQL_RES	*result;
475 	zval		*mysql_result;
476 	zend_long		mode = MYSQLI_NUM;
477 
478 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|l", &mysql_result, mysqli_result_class_entry, &mode) == FAILURE) {
479 		RETURN_THROWS();
480 	}
481 	MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
482 
483 	if (!mode || (mode & ~MYSQLI_BOTH)) {
484 		zend_argument_value_error(ERROR_ARG_POS(2), "must be one of MYSQLI_NUM, MYSQLI_ASSOC, or MYSQLI_BOTH");
485 		RETURN_THROWS();
486 	}
487 
488 	array_init_size(return_value, mysql_num_rows(result));
489 
490 	zend_ulong i = 0;
491 	do {
492 		zval row;
493 		php_mysqli_fetch_into_hash_aux(&row, result, mode);
494 		if (Z_TYPE(row) != IS_ARRAY) {
495 			zval_ptr_dtor_nogc(&row);
496 			break;
497 		}
498 		add_index_zval(return_value, i++, &row);
499 	} while (1);
500 }
501 /* }}} */
502 
503 #ifdef MYSQLI_USE_MYSQLND
504 /* {{{ Returns statistics about the zval cache */
505 PHP_FUNCTION(mysqli_get_client_stats)
506 {
507 	if (zend_parse_parameters_none() == FAILURE) {
508 		RETURN_THROWS();
509 	}
510 	mysqlnd_get_client_stats(return_value);
511 }
512 /* }}} */
513 
514 /* {{{ Returns statistics about the zval cache */
515 PHP_FUNCTION(mysqli_get_connection_stats)
516 {
517 	MY_MYSQL	*mysql;
518 	zval		*mysql_link;
519 
520 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
521 									 &mysql_link, mysqli_link_class_entry) == FAILURE) {
522 		RETURN_THROWS();
523 	}
524 	MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
525 
526 	mysqlnd_get_connection_stats(mysql->mysql, return_value);
527 }
528 #endif
529 /* }}} */
530 
531 /* {{{ Fetches all client errors */
532 PHP_FUNCTION(mysqli_error_list)
533 {
534 	MY_MYSQL	*mysql;
535 	zval		*mysql_link;
536 
537 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
538 		RETURN_THROWS();
539 	}
540 	MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
541 #ifdef MYSQLI_USE_MYSQLND
542 	if (1) {
543 		MYSQLND_ERROR_LIST_ELEMENT * message;
544 		zend_llist_position pos;
545 		array_init(return_value);
546 		for (message = (MYSQLND_ERROR_LIST_ELEMENT *) zend_llist_get_first_ex(&mysql->mysql->data->error_info->error_list, &pos);
547 			 message;
548 			 message = (MYSQLND_ERROR_LIST_ELEMENT *) zend_llist_get_next_ex(&mysql->mysql->data->error_info->error_list, &pos))
549 		{
550 			zval single_error;
551 			array_init(&single_error);
552 			add_assoc_long_ex(&single_error, "errno", sizeof("errno") - 1, message->error_no);
553 			add_assoc_string_ex(&single_error, "sqlstate", sizeof("sqlstate") - 1, message->sqlstate);
554 			add_assoc_string_ex(&single_error, "error", sizeof("error") - 1, message->error);
555 			add_next_index_zval(return_value, &single_error);
556 		}
557 	} else {
558 		RETURN_EMPTY_ARRAY();
559 	}
560 #else
561 	if (mysql_errno(mysql->mysql)) {
562 		zval single_error;
563 		array_init(return_value);
564 		array_init(&single_error);
565 		add_assoc_long_ex(&single_error, "errno", sizeof("errno") - 1, mysql_errno(mysql->mysql));
566 		add_assoc_string_ex(&single_error, "sqlstate", sizeof("sqlstate") - 1, mysql_sqlstate(mysql->mysql));
567 		add_assoc_string_ex(&single_error, "error", sizeof("error") - 1, mysql_error(mysql->mysql));
568 		add_next_index_zval(return_value, &single_error);
569 	} else {
570 		RETURN_EMPTY_ARRAY();
571 	}
572 #endif
573 }
574 /* }}} */
575 
576 /* {{{ */
577 PHP_FUNCTION(mysqli_stmt_error_list)
578 {
579 	MY_STMT	*stmt;
580 	zval 	*mysql_stmt;
581 
582 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
583 		RETURN_THROWS();
584 	}
585 	MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_INITIALIZED);
586 #ifdef MYSQLI_USE_MYSQLND
587 	if (stmt->stmt && stmt->stmt->data && stmt->stmt->data->error_info) {
588 		MYSQLND_ERROR_LIST_ELEMENT * message;
589 		zend_llist_position pos;
590 		array_init(return_value);
591 		for (message = (MYSQLND_ERROR_LIST_ELEMENT *) zend_llist_get_first_ex(&stmt->stmt->data->error_info->error_list, &pos);
592 			 message;
593 			 message = (MYSQLND_ERROR_LIST_ELEMENT *) zend_llist_get_next_ex(&stmt->stmt->data->error_info->error_list, &pos))
594 		{
595 			zval single_error;
596 			array_init(&single_error);
597 			add_assoc_long_ex(&single_error, "errno", sizeof("errno") - 1, message->error_no);
598 			add_assoc_string_ex(&single_error, "sqlstate", sizeof("sqlstate") - 1, message->sqlstate);
599 			add_assoc_string_ex(&single_error, "error", sizeof("error") - 1, message->error);
600 			add_next_index_zval(return_value, &single_error);
601 		}
602 	} else {
603 		RETURN_EMPTY_ARRAY();
604 	}
605 #else
606 	if (mysql_stmt_errno(stmt->stmt)) {
607 		zval single_error;
608 		array_init(return_value);
609 		array_init(&single_error);
610 		add_assoc_long_ex(&single_error, "errno", sizeof("errno") - 1, mysql_stmt_errno(stmt->stmt));
611 		add_assoc_string_ex(&single_error, "sqlstate", sizeof("sqlstate") - 1, mysql_stmt_sqlstate(stmt->stmt));
612 		add_assoc_string_ex(&single_error, "error", sizeof("error") - 1, mysql_stmt_error(stmt->stmt));
613 		add_next_index_zval(return_value, &single_error);
614 	} else {
615 		RETURN_EMPTY_ARRAY();
616 	}
617 #endif
618 }
619 /* }}} */
620 
621 /* {{{ Fetch a result row as an object */
622 PHP_FUNCTION(mysqli_fetch_object)
623 {
624 	php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQLI_ASSOC, 1);
625 }
626 /* }}} */
627 
628 /* {{{ allows to execute multiple queries  */
629 PHP_FUNCTION(mysqli_multi_query)
630 {
631 	MY_MYSQL	*mysql;
632 	zval		*mysql_link;
633 	char		*query = NULL;
634 	size_t 		query_len;
635 
636 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &mysql_link, mysqli_link_class_entry, &query, &query_len) == FAILURE) {
637 		RETURN_THROWS();
638 	}
639 	MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
640 
641 	MYSQLI_ENABLE_MQ;
642 	if (mysql_real_query(mysql->mysql, query, query_len)) {
643 #ifndef MYSQLI_USE_MYSQLND
644 		char s_error[MYSQL_ERRMSG_SIZE], s_sqlstate[SQLSTATE_LENGTH+1];
645 		unsigned int s_errno;
646 		/* we have to save error information, cause
647 		MYSQLI_DISABLE_MQ will reset error information */
648 		strcpy(s_error, mysql_error(mysql->mysql));
649 		strcpy(s_sqlstate, mysql_sqlstate(mysql->mysql));
650 		s_errno = mysql_errno(mysql->mysql);
651 #else
652 		MYSQLND_ERROR_INFO error_info = *mysql->mysql->data->error_info;
653 		mysql->mysql->data->error_info->error_list.head = NULL;
654 		mysql->mysql->data->error_info->error_list.tail = NULL;
655 		mysql->mysql->data->error_info->error_list.count = 0;
656 #endif
657 		MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
658 		MYSQLI_DISABLE_MQ;
659 
660 #ifndef MYSQLI_USE_MYSQLND
661 		/* restore error information */
662 		strcpy(mysql->mysql->net.last_error, s_error);
663 		strcpy(mysql->mysql->net.sqlstate, s_sqlstate);
664 		mysql->mysql->net.last_errno = s_errno;
665 #else
666 		zend_llist_clean(&mysql->mysql->data->error_info->error_list);
667 		*mysql->mysql->data->error_info = error_info;
668 #endif
669 		RETURN_FALSE;
670 	}
671 	RETURN_TRUE;
672 }
673 /* }}} */
674 
675 /* {{{ */
676 PHP_FUNCTION(mysqli_query)
677 {
678 	MY_MYSQL			*mysql;
679 	zval				*mysql_link;
680 	MYSQLI_RESOURCE		*mysqli_resource;
681 	MYSQL_RES 			*result = NULL;
682 	char				*query = NULL;
683 	size_t 				query_len;
684 	zend_long 				resultmode = MYSQLI_STORE_RESULT;
685 
686 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os|l", &mysql_link, mysqli_link_class_entry, &query, &query_len, &resultmode) == FAILURE) {
687 		RETURN_THROWS();
688 	}
689 
690 	if (!query_len) {
691 		zend_argument_value_error(ERROR_ARG_POS(2), "cannot be empty");
692 		RETURN_THROWS();
693 	}
694 	if ((resultmode & ~MYSQLI_ASYNC) != MYSQLI_USE_RESULT &&
695 		MYSQLI_STORE_RESULT != (resultmode & ~(MYSQLI_ASYNC | MYSQLI_STORE_RESULT_COPY_DATA))
696 	) {
697 		zend_argument_value_error(ERROR_ARG_POS(3), "must be either MYSQLI_USE_RESULT or MYSQLI_STORE_RESULT"
698 			#ifdef MYSQLI_USE_MYSQLND
699 				" with MYSQLI_ASYNC as an optional bitmask flag"
700 			#endif
701 		);
702 		RETURN_THROWS();
703 	}
704 
705 	MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
706 
707 	MYSQLI_DISABLE_MQ;
708 
709 
710 #ifdef MYSQLI_USE_MYSQLND
711 	if (resultmode & MYSQLI_ASYNC) {
712 		if (mysqli_async_query(mysql->mysql, query, query_len)) {
713 			MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
714 			RETURN_FALSE;
715 		}
716 		mysql->async_result_fetch_type = resultmode & ~MYSQLI_ASYNC;
717 		RETURN_TRUE;
718 	}
719 #endif
720 
721 	if (mysql_real_query(mysql->mysql, query, query_len)) {
722 		MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
723 		RETURN_FALSE;
724 	}
725 
726 	if (!mysql_field_count(mysql->mysql)) {
727 		/* no result set - not a SELECT */
728 		if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
729 			php_mysqli_report_index(query, mysqli_server_status(mysql->mysql));
730 		}
731 		RETURN_TRUE;
732 	}
733 
734 #ifdef MYSQLI_USE_MYSQLND
735 	switch (resultmode & ~(MYSQLI_ASYNC | MYSQLI_STORE_RESULT_COPY_DATA)) {
736 #else
737 	switch (resultmode & ~MYSQLI_ASYNC) {
738 #endif
739 		case MYSQLI_STORE_RESULT:
740 			result = mysql_store_result(mysql->mysql);
741 			break;
742 		case MYSQLI_USE_RESULT:
743 			result = mysql_use_result(mysql->mysql);
744 			break;
745 	}
746 	if (!result) {
747 		MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
748 		RETURN_FALSE;
749 	}
750 
751 	if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
752 		php_mysqli_report_index(query, mysqli_server_status(mysql->mysql));
753 	}
754 
755 	mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
756 	mysqli_resource->ptr = (void *)result;
757 	mysqli_resource->status = MYSQLI_STATUS_VALID;
758 	MYSQLI_RETVAL_RESOURCE(mysqli_resource, mysqli_result_class_entry);
759 }
760 /* }}} */
761 
762 #ifdef MYSQLI_USE_MYSQLND
763 #include "php_network.h"
764 /* {{{ mysqlnd_zval_array_to_mysqlnd_array functions */
765 static int mysqlnd_zval_array_to_mysqlnd_array(zval *in_array, MYSQLND ***out_array)
766 {
767 	zval *elem;
768 	int i = 0, current = 0;
769 
770 	if (Z_TYPE_P(in_array) != IS_ARRAY) {
771 		return SUCCESS;
772 	}
773 	*out_array = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(in_array)) + 1, sizeof(MYSQLND *));
774 	ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(in_array), elem) {
775 		i++;
776 		if (Z_TYPE_P(elem) != IS_OBJECT ||
777 			!instanceof_function(Z_OBJCE_P(elem), mysqli_link_class_entry)) {
778 			zend_argument_type_error(i, "must be an instance of mysqli, %s given", zend_zval_type_name(elem));
779 			return FAILURE;
780 		} else {
781 			MY_MYSQL *mysql;
782 			MYSQLI_RESOURCE *my_res;
783 			mysqli_object *intern = Z_MYSQLI_P(elem);
784 			if (!(my_res = (MYSQLI_RESOURCE *)intern->ptr)) {
785 				zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(intern->zo.ce->name));
786 				return FAILURE;
787 		  	}
788 			mysql = (MY_MYSQL*) my_res->ptr;
789 			if (my_res->status < MYSQLI_STATUS_VALID) {
790 				zend_throw_error(NULL, "%s object is not fully initialized", ZSTR_VAL(intern->zo.ce->name));
791 				return FAILURE;
792 			}
793 			(*out_array)[current++] = mysql->mysql;
794 		}
795 	} ZEND_HASH_FOREACH_END();
796 	return SUCCESS;
797 }
798 /* }}} */
799 
800 /* {{{ mysqlnd_zval_array_from_mysqlnd_array */
801 static int mysqlnd_zval_array_from_mysqlnd_array(MYSQLND **in_array, zval *out_array)
802 {
803 	MYSQLND **p = in_array;
804 	zval dest_array;
805 	zval *elem, *dest_elem;
806 	int ret = 0;
807 
808 	array_init_size(&dest_array, zend_hash_num_elements(Z_ARRVAL_P(out_array)));
809 
810 	ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(out_array), elem) {
811 		if (Z_TYPE_P(elem) != IS_OBJECT ||
812 				!instanceof_function(Z_OBJCE_P(elem), mysqli_link_class_entry)) {
813 			continue;
814 		}
815 		{
816 			MY_MYSQL *mysql;
817 			MYSQLI_RESOURCE *my_res;
818 			mysqli_object *intern = Z_MYSQLI_P(elem);
819 			if (!(my_res = (MYSQLI_RESOURCE *)intern->ptr)) {
820 				zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(intern->zo.ce->name));
821 				return FAILURE;
822 		  	}
823 			mysql = (MY_MYSQL *) my_res->ptr;
824 			if (mysql->mysql == *p) {
825 				dest_elem = zend_hash_next_index_insert(Z_ARRVAL(dest_array), elem);
826 				if (dest_elem) {
827 					zval_add_ref(dest_elem);
828 				}
829 				ret++;
830 				p++;
831 			}
832 		}
833 	} ZEND_HASH_FOREACH_END();
834 
835 	/* destroy old array and add new one */
836 	zval_ptr_dtor(out_array);
837 	ZVAL_COPY_VALUE(out_array, &dest_array);
838 
839 	return 0;
840 }
841 /* }}} */
842 
843 /* {{{ mysqlnd_dont_poll_zval_array_from_mysqlnd_array */
844 static int mysqlnd_dont_poll_zval_array_from_mysqlnd_array(MYSQLND **in_array, zval *in_zval_array, zval *out_array)
845 {
846 	MYSQLND **p = in_array;
847 	zval proxy, *elem, *dest_elem;
848 	int ret = 0;
849 
850 	array_init(&proxy);
851 	if (in_array) {
852 		ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(in_zval_array), elem) {
853 			MY_MYSQL *mysql;
854 			mysqli_object *intern = Z_MYSQLI_P(elem);
855 			mysql = (MY_MYSQL *)((MYSQLI_RESOURCE *)intern->ptr)->ptr;
856 			if (mysql->mysql == *p) {
857 				dest_elem = zend_hash_next_index_insert(Z_ARRVAL(proxy), elem);
858 				if (dest_elem) {
859 					zval_add_ref(dest_elem);
860 				}
861 				ret++;
862 				p++;
863 			}
864 		} ZEND_HASH_FOREACH_END();
865 	}
866 
867 	/* destroy old array and add new one */
868 	zval_ptr_dtor(out_array);
869 	ZVAL_COPY_VALUE(out_array, &proxy);
870 
871 	return 0;
872 }
873 /* }}} */
874 
875 /* {{{ Poll connections */
876 PHP_FUNCTION(mysqli_poll)
877 {
878 	zval			*r_array, *e_array, *dont_poll_array;
879 	MYSQLND			**new_r_array = NULL, **new_e_array = NULL, **new_dont_poll_array = NULL;
880 	zend_long			sec = 0, usec = 0;
881 	enum_func_status ret;
882 	int 			desc_num;
883 
884 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "a!a!al|l", &r_array, &e_array, &dont_poll_array, &sec, &usec) == FAILURE) {
885 		RETURN_THROWS();
886 	}
887 	if (sec < 0) {
888 		zend_argument_value_error(4, "must be greater than or equal to 0");
889 		RETURN_THROWS();
890 	}
891 	if (usec < 0) {
892 		zend_argument_value_error(5, "must be greater than or equal to 0");
893 		RETURN_THROWS();
894 	}
895 
896 	// TODO Error promotion
897 	if (!r_array && !e_array) {
898 		php_error_docref(NULL, E_WARNING, "No stream arrays were passed");
899 		RETURN_FALSE;
900 	}
901 
902 	if (r_array != NULL) {
903 		if (mysqlnd_zval_array_to_mysqlnd_array(r_array, &new_r_array) == FAILURE) {
904 			efree(new_r_array);
905 			RETURN_THROWS();
906 		}
907 	}
908 	if (e_array != NULL) {
909 		if (mysqlnd_zval_array_to_mysqlnd_array(e_array, &new_e_array) == FAILURE) {
910 			efree(new_e_array);
911 			efree(new_r_array);
912 			RETURN_THROWS();
913 		}
914 	}
915 
916 	ret = mysqlnd_poll(new_r_array, new_e_array, &new_dont_poll_array, sec, usec, &desc_num);
917 
918 	mysqlnd_dont_poll_zval_array_from_mysqlnd_array(r_array != NULL ? new_dont_poll_array:NULL, r_array, dont_poll_array);
919 
920 	if (r_array != NULL) {
921 		mysqlnd_zval_array_from_mysqlnd_array(new_r_array, r_array);
922 	}
923 	if (e_array != NULL) {
924 		mysqlnd_zval_array_from_mysqlnd_array(new_e_array, e_array);
925 	}
926 
927 	if (new_dont_poll_array) {
928 		efree(new_dont_poll_array);
929 	}
930 	if (new_r_array) {
931 		efree(new_r_array);
932 	}
933 	if (new_e_array) {
934 		efree(new_e_array);
935 	}
936 	if (ret == PASS) {
937 		RETURN_LONG(desc_num);
938 	} else {
939 		RETURN_FALSE;
940 	}
941 }
942 /* }}} */
943 
944 /* {{{ Poll connections */
945 PHP_FUNCTION(mysqli_reap_async_query)
946 {
947 	MY_MYSQL		*mysql;
948 	zval			*mysql_link;
949 	MYSQLI_RESOURCE		*mysqli_resource;
950 	MYSQL_RES 			*result = NULL;
951 
952 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
953 		RETURN_THROWS();
954 	}
955 
956 	MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
957 
958 	if (FAIL == mysqlnd_reap_async_query(mysql->mysql)) {
959 		MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
960 		RETURN_FALSE;
961 	}
962 
963 	if (!mysql_field_count(mysql->mysql)) {
964 		/* no result set - not a SELECT */
965 		if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
966 /*			php_mysqli_report_index("n/a", mysqli_server_status(mysql->mysql)); */
967 		}
968 		RETURN_TRUE;
969 	}
970 
971 	switch (mysql->async_result_fetch_type) {
972 		case MYSQLI_STORE_RESULT:
973 			result = mysql_store_result(mysql->mysql);
974 			break;
975 		case MYSQLI_USE_RESULT:
976 			result = mysql_use_result(mysql->mysql);
977 			break;
978 	}
979 
980 	if (!result) {
981 		MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
982 		RETURN_FALSE;
983 	}
984 
985 	if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
986 /*		php_mysqli_report_index("n/a", mysqli_server_status(mysql->mysql)); */
987 	}
988 
989 	mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
990 	mysqli_resource->ptr = (void *)result;
991 	mysqli_resource->status = MYSQLI_STATUS_VALID;
992 	MYSQLI_RETVAL_RESOURCE(mysqli_resource, mysqli_result_class_entry);
993 }
994 /* }}} */
995 
996 /* {{{ Buffer result set on client */
997 PHP_FUNCTION(mysqli_stmt_get_result)
998 {
999 	MYSQL_RES 		*result;
1000 	MYSQLI_RESOURCE	*mysqli_resource;
1001 	MY_STMT			*stmt;
1002 	zval 			*mysql_stmt;
1003 
1004 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
1005 		RETURN_THROWS();
1006 	}
1007 	MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
1008 
1009 	if (!(result = mysqlnd_stmt_get_result(stmt->stmt))) {
1010 		MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
1011 		RETURN_FALSE;
1012 	}
1013 
1014 	mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
1015 	mysqli_resource->ptr = (void *)result;
1016 	mysqli_resource->status = MYSQLI_STATUS_VALID;
1017 	MYSQLI_RETVAL_RESOURCE(mysqli_resource, mysqli_result_class_entry);
1018 }
1019 /* }}} */
1020 #endif
1021 
1022 /* {{{ */
1023 PHP_FUNCTION(mysqli_get_warnings)
1024 {
1025 	MY_MYSQL			*mysql;
1026 	zval				*mysql_link;
1027 	MYSQLI_RESOURCE		*mysqli_resource;
1028 	MYSQLI_WARNING		*w = NULL;
1029 
1030 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
1031 		RETURN_THROWS();
1032 	}
1033 	MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
1034 
1035 	if (mysql_warning_count(mysql->mysql)) {
1036 #ifdef MYSQLI_USE_MYSQLND
1037 		w = php_get_warnings(mysql->mysql->data);
1038 #else
1039 		w = php_get_warnings(mysql->mysql);
1040 #endif
1041 	}
1042 	if (!w) {
1043 		RETURN_FALSE;
1044 	}
1045 	mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
1046 	mysqli_resource->ptr = mysqli_resource->info = (void *)w;
1047 	mysqli_resource->status = MYSQLI_STATUS_VALID;
1048 	MYSQLI_RETVAL_RESOURCE(mysqli_resource, mysqli_warning_class_entry);
1049 }
1050 /* }}} */
1051 
1052 /* {{{ */
1053 PHP_FUNCTION(mysqli_stmt_get_warnings)
1054 {
1055 	MY_STMT				*stmt;
1056 	zval				*stmt_link;
1057 	MYSQLI_RESOURCE		*mysqli_resource;
1058 	MYSQLI_WARNING		*w = NULL;
1059 
1060 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &stmt_link, mysqli_stmt_class_entry) == FAILURE) {
1061 		RETURN_THROWS();
1062 	}
1063 	MYSQLI_FETCH_RESOURCE_STMT(stmt, stmt_link, MYSQLI_STATUS_VALID);
1064 
1065 	if (mysqli_stmt_warning_count(stmt->stmt)) {
1066 		w = php_get_warnings(mysqli_stmt_get_connection(stmt->stmt));
1067 	}
1068 	if (!w) {
1069 		RETURN_FALSE;
1070 	}
1071 	mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
1072 	mysqli_resource->ptr = mysqli_resource->info = (void *)w;
1073 	mysqli_resource->status = MYSQLI_STATUS_VALID;
1074 	MYSQLI_RETVAL_RESOURCE(mysqli_resource, mysqli_warning_class_entry);
1075 }
1076 /* }}} */
1077 
1078 /* {{{ sets client character set */
1079 PHP_FUNCTION(mysqli_set_charset)
1080 {
1081 	MY_MYSQL	*mysql;
1082 	zval		*mysql_link;
1083 	char		*cs_name;
1084 	size_t		csname_len;
1085 
1086 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &mysql_link, mysqli_link_class_entry, &cs_name, &csname_len) == FAILURE) {
1087 		RETURN_THROWS();
1088 	}
1089 	MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
1090 
1091 	if (mysql_set_character_set(mysql->mysql, cs_name)) {
1092 		MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
1093 		RETURN_FALSE;
1094 	}
1095 	RETURN_TRUE;
1096 }
1097 /* }}} */
1098 
1099 /* {{{ returns a character set object */
1100 PHP_FUNCTION(mysqli_get_charset)
1101 {
1102 	MY_MYSQL				*mysql;
1103 	zval					*mysql_link;
1104 	const char 				*name = NULL, *collation = NULL, *dir = NULL, *comment = NULL;
1105 	uint32_t				minlength, maxlength, number, state;
1106 #ifndef MYSQLI_USE_MYSQLND
1107 	MY_CHARSET_INFO			cs;
1108 #else
1109 	const MYSQLND_CHARSET	*cs;
1110 #endif
1111 
1112 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
1113 		RETURN_THROWS();
1114 	}
1115 	MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
1116 
1117 
1118 #ifndef MYSQLI_USE_MYSQLND
1119 	mysql_get_character_set_info(mysql->mysql, &cs);
1120 	name = (char *)cs.csname;
1121 	collation = (char *)cs.name;
1122 	dir = (char *)cs.dir;
1123 	minlength = cs.mbminlen;
1124 	maxlength = cs.mbmaxlen;
1125 	number = cs.number;
1126 	state = cs.state;
1127 	comment = cs.comment;
1128 #else
1129 	cs = mysql->mysql->data->charset;
1130 	if (!cs) {
1131 		php_error_docref(NULL, E_WARNING, "The connection has no charset associated");
1132 		RETURN_NULL();
1133 	}
1134 	name = cs->name;
1135 	collation = cs->collation;
1136 	minlength = cs->char_minlen;
1137 	maxlength = cs->char_maxlen;
1138 	number = cs->nr;
1139 	comment = cs->comment;
1140 	state = 1;	/* all charsets are compiled in */
1141 #endif
1142 	object_init(return_value);
1143 
1144 	add_property_string(return_value, "charset", (name) ? (char *)name : "");
1145 	add_property_string(return_value, "collation",(collation) ? (char *)collation : "");
1146 	add_property_string(return_value, "dir", (dir) ? (char *)dir : "");
1147 	add_property_long(return_value, "min_length", minlength);
1148 	add_property_long(return_value, "max_length", maxlength);
1149 	add_property_long(return_value, "number", number);
1150 	add_property_long(return_value, "state", state);
1151 	add_property_string(return_value, "comment", (comment) ? (char *)comment : "");
1152 }
1153 /* }}} */
1154 
1155 #ifndef MYSQLI_USE_MYSQLND
1156 extern char * mysqli_escape_string_for_tx_name_in_comment(const char * const name);
1157 
1158 /* {{{ */
1159 static int mysqli_begin_transaction_libmysql(MYSQL * conn, const unsigned int mode, const char * const name)
1160 {
1161 	int ret;
1162 	smart_str tmp_str = {0};
1163 	char * name_esc;
1164 	char * query;
1165 	unsigned int query_len;
1166 	if (mode & TRANS_START_WITH_CONSISTENT_SNAPSHOT) {
1167 		if (tmp_str.s) {
1168 			smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
1169 		}
1170 		smart_str_appendl(&tmp_str, "WITH CONSISTENT SNAPSHOT", sizeof("WITH CONSISTENT SNAPSHOT") - 1);
1171 	}
1172 	if (mode & TRANS_START_READ_WRITE) {
1173 		if (tmp_str.s) {
1174 			smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
1175 		}
1176 		smart_str_appendl(&tmp_str, "READ WRITE", sizeof("READ WRITE") - 1);
1177 	} else if (mode & TRANS_START_READ_ONLY) {
1178 		if (tmp_str.s) {
1179 			smart_str_appendl(&tmp_str, ", ", sizeof(", ") - 1);
1180 		}
1181 		smart_str_appendl(&tmp_str, "READ ONLY", sizeof("READ ONLY") - 1);
1182 	}
1183 	smart_str_0(&tmp_str);
1184 
1185 	name_esc = mysqli_escape_string_for_tx_name_in_comment(name);
1186 	query_len = spprintf(&query, 0, "START TRANSACTION%s %s",
1187 						 name_esc? name_esc:"", tmp_str.s? ZSTR_VAL(tmp_str.s):"");
1188 
1189 	smart_str_free(&tmp_str);
1190 	if (name_esc) {
1191 		efree(name_esc);
1192 	}
1193 
1194 	ret = mysql_real_query(conn, query, query_len);
1195 	efree(query);
1196 
1197 	if (ret && mode & (TRANS_START_READ_WRITE | TRANS_START_READ_ONLY) && mysql_errno(conn) == 1064) {
1198 		php_error_docref(NULL, E_WARNING, "This server version doesn't support 'READ WRITE' and 'READ ONLY'. Minimum 5.6.5 is required");
1199 	}
1200 	return ret;
1201 }
1202 /* }}} */
1203 #endif
1204 
1205 /* {{{ Starts a transaction */
1206 PHP_FUNCTION(mysqli_begin_transaction)
1207 {
1208 	MY_MYSQL	*mysql;
1209 	zval		*mysql_link;
1210 	zend_long		flags = TRANS_START_NO_OPT;
1211 	char *		name = NULL;
1212 	size_t			name_len = 0;
1213 
1214 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|ls!", &mysql_link, mysqli_link_class_entry, &flags, &name, &name_len) == FAILURE) {
1215 		RETURN_THROWS();
1216 	}
1217 	MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
1218 	if (flags < 0) {
1219 		zend_argument_value_error(ERROR_ARG_POS(2), "must be one of the MYSQLI_TRANS_* constants");
1220 		RETURN_THROWS();
1221 	}
1222 	if (name && !name_len) {
1223 		zend_argument_value_error(ERROR_ARG_POS(3), "cannot be empty");
1224 		RETURN_THROWS();
1225 	}
1226 
1227 #ifndef MYSQLI_USE_MYSQLND
1228 	if (mysqli_begin_transaction_libmysql(mysql->mysql, flags, name)) {
1229 		RETURN_FALSE;
1230 	}
1231 #else
1232 	if (FAIL == mysqlnd_begin_transaction(mysql->mysql, flags, name)) {
1233 		RETURN_FALSE;
1234 	}
1235 #endif
1236 	RETURN_TRUE;
1237 }
1238 /* }}} */
1239 
1240 #ifndef MYSQLI_USE_MYSQLND
1241 /* {{{ */
1242 static int mysqli_savepoint_libmysql(MYSQL * conn, const char * const name, bool release)
1243 {
1244 	int ret;
1245 	char * query;
1246 	unsigned int query_len = spprintf(&query, 0, release? "RELEASE SAVEPOINT `%s`":"SAVEPOINT `%s`", name);
1247 	ret = mysql_real_query(conn, query, query_len);
1248 	efree(query);
1249 	return ret;
1250 }
1251 /* }}} */
1252 #endif
1253 
1254 /* {{{ Starts a transaction */
1255 PHP_FUNCTION(mysqli_savepoint)
1256 {
1257 	MY_MYSQL	*mysql;
1258 	zval		*mysql_link;
1259 	char *		name = NULL;
1260 	size_t			name_len;
1261 
1262 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &mysql_link, mysqli_link_class_entry, &name, &name_len) == FAILURE) {
1263 		RETURN_THROWS();
1264 	}
1265 	MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
1266 	if (name_len == 0) {
1267 		zend_argument_value_error(ERROR_ARG_POS(2), "cannot be empty");
1268 		RETURN_THROWS();
1269 	}
1270 
1271 #ifndef MYSQLI_USE_MYSQLND
1272 	if (mysqli_savepoint_libmysql(mysql->mysql, name, false)) {
1273 #else
1274 	if (FAIL == mysqlnd_savepoint(mysql->mysql, name)) {
1275 #endif
1276 		RETURN_FALSE;
1277 	}
1278 	RETURN_TRUE;
1279 }
1280 /* }}} */
1281 
1282 /* {{{ Starts a transaction */
1283 PHP_FUNCTION(mysqli_release_savepoint)
1284 {
1285 	MY_MYSQL	*mysql;
1286 	zval		*mysql_link;
1287 	char *		name = NULL;
1288 	size_t			name_len;
1289 
1290 	if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &mysql_link, mysqli_link_class_entry, &name, &name_len) == FAILURE) {
1291 		RETURN_THROWS();
1292 	}
1293 	MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
1294 	if (name_len == 0) {
1295 		zend_argument_value_error(ERROR_ARG_POS(2), "cannot be empty");
1296 		RETURN_THROWS();
1297 	}
1298 #ifndef MYSQLI_USE_MYSQLND
1299 	if (mysqli_savepoint_libmysql(mysql->mysql, name, true)) {
1300 #else
1301 	if (FAIL == mysqlnd_release_savepoint(mysql->mysql, name)) {
1302 #endif
1303 		RETURN_FALSE;
1304 	}
1305 	RETURN_TRUE;
1306 }
1307 /* }}} */
1308 
1309 /* {{{ Returns information about open and cached links */
1310 PHP_FUNCTION(mysqli_get_links_stats)
1311 {
1312 	if (zend_parse_parameters_none() == FAILURE) {
1313 		RETURN_THROWS();
1314 	}
1315 
1316 	array_init(return_value);
1317 	add_assoc_long_ex(return_value, "total", sizeof("total") - 1, MyG(num_links));
1318 	add_assoc_long_ex(return_value, "active_plinks", sizeof("active_plinks") - 1, MyG(num_active_persistent));
1319 	add_assoc_long_ex(return_value, "cached_plinks", sizeof("cached_plinks") - 1, MyG(num_inactive_persistent));
1320 }
1321 /* }}} */
1322