PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/mysqli - mysqli_nonapi.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 448 488 91.8 %
Date: 2014-07-21 Functions: 30 30 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Tue, 22 Jul 2014 01:33:12 +0000 (10 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.