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_api.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 666 707 94.2 %
Date: 2015-06-27 Functions: 81 83 97.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | PHP Version 7                                                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 1997-2015 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 "php_globals.h"
      32             : #include "ext/standard/info.h"
      33             : #include "zend_smart_str.h"
      34             : #include "php_mysqli_structs.h"
      35             : #include "mysqli_priv.h"
      36             : #include "ext/mysqlnd/mysql_float_to_double.h"
      37             : 
      38             : 
      39             : #if !defined(MYSQLI_USE_MYSQLND)
      40             : /* {{{ mysqli_tx_cor_options_to_string */
      41             : static void mysqli_tx_cor_options_to_string(const MYSQL * const conn, smart_str * str, const uint32_t mode)
      42             : {
      43             :         if (mode & TRANS_COR_AND_CHAIN && !(mode & TRANS_COR_AND_NO_CHAIN)) {
      44             :                 if (str->s && str->s->len) {
      45             :                         smart_str_appendl(str, " ", sizeof(" ") - 1);
      46             :                 }
      47             :                 smart_str_appendl(str, "AND CHAIN", sizeof("AND CHAIN") - 1);
      48             :         } else if (mode & TRANS_COR_AND_NO_CHAIN && !(mode & TRANS_COR_AND_CHAIN)) {
      49             :                 if (str->s && str->s->len) {
      50             :                         smart_str_appendl(str, " ", sizeof(" ") - 1);
      51             :                 }
      52             :                 smart_str_appendl(str, "AND NO CHAIN", sizeof("AND NO CHAIN") - 1);
      53             :         }
      54             : 
      55             :         if (mode & TRANS_COR_RELEASE && !(mode & TRANS_COR_NO_RELEASE)) {
      56             :                 if (str->s && str->s->len) {
      57             :                         smart_str_appendl(str, " ", sizeof(" ") - 1);
      58             :                 }
      59             :                 smart_str_appendl(str, "RELEASE", sizeof("RELEASE") - 1);
      60             :         } else if (mode & TRANS_COR_NO_RELEASE && !(mode & TRANS_COR_RELEASE)) {
      61             :                 if (str->s && str->s->len) {
      62             :                         smart_str_appendl(str, " ", sizeof(" ") - 1);
      63             :                 }
      64             :                 smart_str_appendl(str, "NO RELEASE", sizeof("NO RELEASE") - 1);
      65             :         }
      66             :         smart_str_0(str);
      67             : }
      68             : /* }}} */
      69             : 
      70             : /* {{{ mysqlnd_escape_string_for_tx_name_in_comment */
      71             : char *
      72             : mysqli_escape_string_for_tx_name_in_comment(const char * const name)
      73             : {
      74             :         char * ret = NULL;
      75             :         if (name) {
      76             :                 zend_bool warned = FALSE;
      77             :                 const char * p_orig = name;
      78             :                 char * p_copy;
      79             :                 p_copy = ret = emalloc(strlen(name) + 1 + 2 + 2 + 1); /* space, open, close, NullS */
      80             :                 *p_copy++ = ' ';
      81             :                 *p_copy++ = '/';
      82             :                 *p_copy++ = '*';
      83             :                 while (1) {
      84             :                         register char v = *p_orig;
      85             :                         if (v == 0) {
      86             :                                 break;
      87             :                         }
      88             :                         if ((v >= '0' && v <= '9') ||
      89             :                                 (v >= 'a' && v <= 'z') ||
      90             :                                 (v >= 'A' && v <= 'Z') ||
      91             :                                 v == '-' ||
      92             :                                 v == '_' ||
      93             :                                 v == ' ' ||
      94             :                                 v == '=')
      95             :                         {
      96             :                                 *p_copy++ = v;
      97             :                         } else if (warned == FALSE) {
      98             :                                 php_error_docref(NULL, E_WARNING, "Transaction name truncated. Must be only [0-9A-Za-z\\-_=]+");
      99             :                                 warned = TRUE;
     100             :                         }
     101             :                         ++p_orig;
     102             :                 }
     103             :                 *p_copy++ = '*';
     104             :                 *p_copy++ = '/';
     105             :                 *p_copy++ = 0;
     106             :         }
     107             :         return ret;
     108             : }
     109             : /* }}} */
     110             : 
     111             : /* {{{ mysqli_commit_or_rollback_libmysql */
     112             : static int mysqli_commit_or_rollback_libmysql(MYSQL * conn, zend_bool commit, const uint32_t mode, const char * const name)
     113             : {
     114             :         int ret;
     115             :         smart_str tmp_str = {0};
     116             :         mysqli_tx_cor_options_to_string(conn, &tmp_str, mode);
     117             :         smart_str_0(&tmp_str);
     118             : 
     119             :         {
     120             :                 char *query;
     121             :                 char *name_esc = mysqli_escape_string_for_tx_name_in_comment(name);
     122             :                 size_t query_len;
     123             : 
     124             :                 query_len = spprintf(&query, 0,
     125             :                                 (commit? "COMMIT%s %s":"ROLLBACK%s %s"), name_esc? name_esc:"", tmp_str.s? tmp_str.s->val:"");
     126             :                 smart_str_free(&tmp_str);
     127             :                 if (name_esc) {
     128             :                         efree(name_esc);
     129             :                         name_esc = NULL;
     130             :                 }
     131             : 
     132             :                 ret = mysql_real_query(conn, query, query_len);
     133             :                 efree(query);
     134             :         }
     135             :         return ret;
     136             : }
     137             : /* }}} */
     138             : #endif
     139             : 
     140             : /* {{{ proto mixed mysqli_affected_rows(object link)
     141             :    Get number of affected rows in previous MySQL operation */
     142          27 : PHP_FUNCTION(mysqli_affected_rows)
     143             : {
     144             :         MY_MYSQL                *mysql;
     145             :         zval                    *mysql_link;
     146             :         my_ulonglong    rc;
     147             : 
     148          27 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
     149           3 :                 return;
     150             :         }
     151             : 
     152          24 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
     153             : 
     154          17 :         rc = mysql_affected_rows(mysql->mysql);
     155          17 :         if (rc == (my_ulonglong) -1) {
     156           1 :                 RETURN_LONG(-1);
     157             :         }
     158          16 :         MYSQLI_RETURN_LONG_INT(rc);
     159             : }
     160             : /* }}} */
     161             : 
     162             : /* {{{ proto bool mysqli_autocommit(object link, bool mode)
     163             :    Turn auto commit on or of */
     164          33 : PHP_FUNCTION(mysqli_autocommit)
     165             : {
     166             :         MY_MYSQL        *mysql;
     167             :         zval            *mysql_link;
     168             :         zend_bool       automode;
     169             : 
     170          33 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ob", &mysql_link, mysqli_link_class_entry, &automode) == FAILURE) {
     171           3 :                 return;
     172             :         }
     173          30 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
     174             : 
     175          28 :         if (mysql_autocommit(mysql->mysql, (my_bool)automode)) {
     176           0 :                 RETURN_FALSE;
     177             :         }
     178          28 :         RETURN_TRUE;
     179             : }
     180             : /* }}} */
     181             : 
     182             : /* {{{ mysqli_stmt_bind_param_do_bind */
     183             : #ifndef MYSQLI_USE_MYSQLND
     184             : static
     185             : int mysqli_stmt_bind_param_do_bind(MY_STMT *stmt, unsigned int argc, unsigned int num_vars,
     186             :                                                                    zval *args, unsigned int start, const char * const types)
     187             : {
     188             :         int                             i, ofs;
     189             :         MYSQL_BIND              *bind;
     190             :         unsigned long   rc;
     191             : 
     192             :         /* prevent leak if variables are already bound */
     193             :         if (stmt->param.var_cnt) {
     194             :                 php_free_stmt_bind_buffer(stmt->param, FETCH_SIMPLE);
     195             :         }
     196             : 
     197             :         stmt->param.is_null = ecalloc(num_vars, sizeof(char));
     198             :         bind = (MYSQL_BIND *) ecalloc(num_vars, sizeof(MYSQL_BIND));
     199             : 
     200             :         ofs = 0;
     201             :         for (i = start; i < argc; i++) {
     202             :                 zval *param;
     203             :                 if (Z_ISREF(args[i])) {
     204             :                         param = Z_REFVAL(args[i]);
     205             :                 } else {
     206             :                         param = &args[i];
     207             :                 }
     208             :                 /* set specified type */
     209             :                 switch (types[ofs]) {
     210             :                         case 'd': /* Double */
     211             :                                 bind[ofs].buffer_type = MYSQL_TYPE_DOUBLE;
     212             :                                 bind[ofs].buffer = &Z_DVAL_P(param);
     213             :                                 bind[ofs].is_null = &stmt->param.is_null[ofs];
     214             :                                 break;
     215             : 
     216             :                         case 'i': /* Integer */
     217             : #if SIZEOF_ZEND_LONG==8
     218             :                                 bind[ofs].buffer_type = MYSQL_TYPE_LONGLONG;
     219             : #elif SIZEOF_ZEND_LONG==4
     220             :                                 bind[ofs].buffer_type = MYSQL_TYPE_LONG;
     221             : #endif
     222             :                                 bind[ofs].buffer = &Z_LVAL_P(param);
     223             :                                 bind[ofs].is_null = &stmt->param.is_null[ofs];
     224             :                                 break;
     225             : 
     226             :                         case 'b': /* Blob (send data) */
     227             :                                 bind[ofs].buffer_type = MYSQL_TYPE_LONG_BLOB;
     228             :                                 /* don't initialize is_null and length to 0 because we use ecalloc */
     229             :                                 break;
     230             : 
     231             :                         case 's': /* string */
     232             :                                 bind[ofs].buffer_type = MYSQL_TYPE_VAR_STRING;
     233             :                                 /* don't initialize buffer and buffer_length because we use ecalloc */
     234             :                                 bind[ofs].is_null = &stmt->param.is_null[ofs];
     235             :                                 break;
     236             : 
     237             :                         default:
     238             :                                 php_error_docref(NULL, E_WARNING, "Undefined fieldtype %c (parameter %d)", types[ofs], i+1);
     239             :                                 rc = 1;
     240             :                                 goto end_1;
     241             :                 }
     242             :                 ofs++;
     243             :         }
     244             :         rc = mysql_stmt_bind_param(stmt->stmt, bind);
     245             : 
     246             : end_1:
     247             :         if (rc) {
     248             :                 efree(stmt->param.is_null);
     249             :         } else {
     250             :                 stmt->param.var_cnt = num_vars;
     251             :                 stmt->param.vars = safe_emalloc(num_vars, sizeof(zval), 0);
     252             :                 for (i = 0; i < num_vars; i++) {
     253             :                         if (bind[i].buffer_type != MYSQL_TYPE_LONG_BLOB) {
     254             :                                 ZVAL_COPY(&stmt->param.vars[i], &args[i+start]);
     255             :                         } else {
     256             :                                 ZVAL_UNDEF(&stmt->param.vars[i]);
     257             :                         }
     258             :                 }
     259             :         }
     260             :         efree(bind);
     261             : 
     262             :         return rc;
     263             : }
     264             : #else
     265             : static
     266       20879 : int mysqli_stmt_bind_param_do_bind(MY_STMT *stmt, unsigned int argc, unsigned int num_vars,
     267             :                                                                    zval *args, unsigned int start, const char * const types)
     268             : {
     269             :         unsigned int i;
     270             :         MYSQLND_PARAM_BIND      *params;
     271       20879 :         enum_func_status        ret = FAIL;
     272             : 
     273             :         /* If no params -> skip binding and return directly */
     274       20879 :         if (argc == start) {
     275           0 :                 return PASS;
     276             :         }
     277       20879 :         params = mysqlnd_stmt_alloc_param_bind(stmt->stmt);
     278       20879 :         if (!params) {
     279           0 :                 goto end;
     280             :         }
     281      195680 :         for (i = 0; i < (argc - start); i++) {
     282             :                 zend_uchar type;
     283      174803 :                 switch (types[i]) {
     284             :                         case 'd': /* Double */
     285         976 :                                 type = MYSQL_TYPE_DOUBLE;
     286         976 :                                 break;
     287             :                         case 'i': /* Integer */
     288             : #if SIZEOF_ZEND_LONG==8
     289      153547 :                                 type = MYSQL_TYPE_LONGLONG;
     290             : #elif SIZEOF_ZEND_LONG==4
     291             :                                 type = MYSQL_TYPE_LONG;
     292             : #endif
     293      153547 :                                 break;
     294             :                         case 'b': /* Blob (send data) */
     295          24 :                                 type = MYSQL_TYPE_LONG_BLOB;
     296          24 :                                 break;
     297             :                         case 's': /* string */
     298       20254 :                                 type = MYSQL_TYPE_VAR_STRING;
     299       20254 :                                 break;
     300             :                         default:
     301             :                                 /* We count parameters from 1 */
     302           2 :                                 php_error_docref(NULL, E_WARNING, "Undefined fieldtype %c (parameter %d)", types[i], i + start + 1);
     303           2 :                                 ret = FAIL;
     304           2 :                                 mysqlnd_stmt_free_param_bind(stmt->stmt, params);
     305           2 :                                 goto end;
     306             :                 }
     307      174801 :                 ZVAL_COPY_VALUE(&params[i].zv, &args[i + start]);
     308      174801 :                 params[i].type = type;
     309             :         }
     310       20877 :         ret = mysqlnd_stmt_bind_param(stmt->stmt, params);
     311             : 
     312             : end:
     313       20879 :         return ret;
     314             : }
     315             : #endif
     316             : /* }}} */
     317             : 
     318             : /* {{{ proto bool mysqli_stmt_bind_param(object stmt, string types, mixed variable [,mixed ...])
     319             :    Bind variables to a prepared statement as parameters */
     320       20890 : PHP_FUNCTION(mysqli_stmt_bind_param)
     321             : {
     322             :         zval                    *args;
     323       20890 :         int                             argc = ZEND_NUM_ARGS();
     324             :         int                             num_vars;
     325       20890 :         int                             start = 2;
     326             :         MY_STMT                 *stmt;
     327             :         zval                    *mysql_stmt;
     328             :         char                    *types;
     329             :         size_t                  types_len;
     330             :         zend_ulong      rc;
     331             : 
     332             :         /* calculate and check number of parameters */
     333       20890 :         if (argc < 2) {
     334             :                 /* there has to be at least one pair */
     335           3 :                 WRONG_PARAM_COUNT;
     336             :         }
     337             : 
     338       20887 :         if (zend_parse_method_parameters((getThis()) ? 1:2, getThis(), "Os", &mysql_stmt, mysqli_stmt_class_entry,
     339             :                                                                         &types, &types_len) == FAILURE) {
     340           1 :                 return;
     341             :         }
     342             : 
     343       20886 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
     344             : 
     345       20886 :         num_vars = argc - 1;
     346       20910 :         if (getThis()) {
     347          24 :                 start = 1;
     348             :         } else {
     349             :                 /* ignore handle parameter in procedural interface*/
     350       20862 :                 --num_vars;
     351             :         }
     352       20886 :         if (!types_len) {
     353           1 :                 php_error_docref(NULL, E_WARNING, "Invalid type or no types specified");
     354           1 :                 RETURN_FALSE;
     355             :         }
     356             : 
     357       20885 :         if (types_len != argc - start) {
     358             :                 /* number of bind variables doesn't match number of elements in type definition string */
     359           3 :                 php_error_docref(NULL, E_WARNING, "Number of elements in type definition string doesn't match number of bind variables");
     360           3 :                 RETURN_FALSE;
     361             :         }
     362             : 
     363       20882 :         if (types_len != mysql_stmt_param_count(stmt->stmt)) {
     364           3 :                 php_error_docref(NULL, E_WARNING, "Number of variables doesn't match number of parameters in prepared statement");
     365           3 :                 RETURN_FALSE;
     366             :         }
     367             : 
     368       20879 :         args = safe_emalloc(argc, sizeof(zval), 0);
     369             : 
     370       20879 :         if (zend_get_parameters_array_ex(argc, args) == FAILURE) {
     371           0 :                 zend_wrong_param_count();
     372           0 :                 rc = 1;
     373             :         } else {
     374       20879 :                 rc = mysqli_stmt_bind_param_do_bind(stmt, argc, num_vars, args, start, types);
     375       20879 :                 MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
     376             :         }
     377             : 
     378       20879 :         efree(args);
     379             : 
     380       20879 :         RETURN_BOOL(!rc);
     381             : }
     382             : /* }}} */
     383             : 
     384             : /* {{{ mysqli_stmt_bind_result_do_bind */
     385             : #ifndef MYSQLI_USE_MYSQLND
     386             : /* TODO:
     387             :    do_alloca, free_alloca
     388             : */
     389             : static int
     390             : mysqli_stmt_bind_result_do_bind(MY_STMT *stmt, zval *args, unsigned int argc)
     391             : {
     392             :         MYSQL_BIND      *bind;
     393             :         int                     i, ofs;
     394             :         int                     var_cnt = argc;
     395             :         zend_long               col_type;
     396             :         zend_ulong              rc;
     397             : 
     398             :         /* prevent leak if variables are already bound */
     399             :         if (stmt->result.var_cnt) {
     400             :                 php_free_stmt_bind_buffer(stmt->result, FETCH_RESULT);
     401             :         }
     402             : 
     403             :         bind = (MYSQL_BIND *)ecalloc(var_cnt, sizeof(MYSQL_BIND));
     404             :         {
     405             :                 int size;
     406             :                 char *p = emalloc(size= var_cnt * (sizeof(char) + sizeof(VAR_BUFFER)));
     407             :                 stmt->result.buf = (VAR_BUFFER *) p;
     408             :                 stmt->result.is_null = p + var_cnt * sizeof(VAR_BUFFER);
     409             :                 memset(p, 0, size);
     410             :         }
     411             : 
     412             :         for (i = 0; i < var_cnt; i++) {
     413             :                 ofs = i;
     414             :                 col_type = (stmt->stmt->fields) ? stmt->stmt->fields[ofs].type : MYSQL_TYPE_STRING;
     415             : 
     416             :                 switch (col_type) {
     417             :                         case MYSQL_TYPE_FLOAT:
     418             :                                 stmt->result.buf[ofs].type = IS_DOUBLE;
     419             :                                 stmt->result.buf[ofs].buflen = sizeof(float);
     420             : 
     421             :                                 stmt->result.buf[ofs].val = (char *)emalloc(sizeof(float));
     422             :                                 bind[ofs].buffer_type = MYSQL_TYPE_FLOAT;
     423             :                                 bind[ofs].buffer = stmt->result.buf[ofs].val;
     424             :                                 bind[ofs].is_null = &stmt->result.is_null[ofs];
     425             :                                 break;
     426             : 
     427             :                         case MYSQL_TYPE_DOUBLE:
     428             :                                 stmt->result.buf[ofs].type = IS_DOUBLE;
     429             :                                 stmt->result.buf[ofs].buflen = sizeof(double);
     430             : 
     431             :                                 /* allocate buffer for double */
     432             :                                 stmt->result.buf[ofs].val = (char *)emalloc(sizeof(double));
     433             :                                 bind[ofs].buffer_type = MYSQL_TYPE_DOUBLE;
     434             :                                 bind[ofs].buffer = stmt->result.buf[ofs].val;
     435             :                                 bind[ofs].is_null = &stmt->result.is_null[ofs];
     436             :                                 break;
     437             : 
     438             :                         case MYSQL_TYPE_NULL:
     439             :                                 stmt->result.buf[ofs].type = IS_NULL;
     440             :                                 /*
     441             :                                   don't initialize to 0 :
     442             :                                   1. stmt->result.buf[ofs].buflen
     443             :                                   2. bind[ofs].buffer
     444             :                                   3. bind[ofs].buffer_length
     445             :                                   because memory was allocated with ecalloc
     446             :                                 */
     447             :                                 bind[ofs].buffer_type = MYSQL_TYPE_NULL;
     448             :                                 bind[ofs].is_null = &stmt->result.is_null[ofs];
     449             :                                 break;
     450             : 
     451             :                         case MYSQL_TYPE_SHORT:
     452             :                         case MYSQL_TYPE_TINY:
     453             :                         case MYSQL_TYPE_LONG:
     454             :                         case MYSQL_TYPE_INT24:
     455             :                         case MYSQL_TYPE_YEAR:
     456             :                                 stmt->result.buf[ofs].type = IS_LONG;
     457             :                                 /* don't set stmt->result.buf[ofs].buflen to 0, we used ecalloc */
     458             :                                 stmt->result.buf[ofs].val = (char *)emalloc(sizeof(int));
     459             :                                 bind[ofs].buffer_type = MYSQL_TYPE_LONG;
     460             :                                 bind[ofs].buffer = stmt->result.buf[ofs].val;
     461             :                                 bind[ofs].is_null = &stmt->result.is_null[ofs];
     462             :                                 bind[ofs].is_unsigned = (stmt->stmt->fields[ofs].flags & UNSIGNED_FLAG) ? 1 : 0;
     463             :                                 break;
     464             : 
     465             :                         case MYSQL_TYPE_LONGLONG:
     466             : #if MYSQL_VERSION_ID > 50002 || defined(MYSQLI_USE_MYSQLND)
     467             :                         case MYSQL_TYPE_BIT:
     468             : #endif
     469             :                                 stmt->result.buf[ofs].type = IS_STRING;
     470             :                                 stmt->result.buf[ofs].buflen = sizeof(my_ulonglong);
     471             :                                 stmt->result.buf[ofs].val = (char *)emalloc(stmt->result.buf[ofs].buflen);
     472             :                                 bind[ofs].buffer_type = col_type;
     473             :                                 bind[ofs].buffer = stmt->result.buf[ofs].val;
     474             :                                 bind[ofs].is_null = &stmt->result.is_null[ofs];
     475             :                                 bind[ofs].buffer_length = stmt->result.buf[ofs].buflen;
     476             :                                 bind[ofs].is_unsigned = (stmt->stmt->fields[ofs].flags & UNSIGNED_FLAG) ? 1 : 0;
     477             :                                 bind[ofs].length = &stmt->result.buf[ofs].output_len;
     478             :                                 break;
     479             : 
     480             :                         case MYSQL_TYPE_DATE:
     481             :                         case MYSQL_TYPE_TIME:
     482             :                         case MYSQL_TYPE_DATETIME:
     483             :                         case MYSQL_TYPE_NEWDATE:
     484             :                         case MYSQL_TYPE_VAR_STRING:
     485             :                         case MYSQL_TYPE_STRING:
     486             :                         case MYSQL_TYPE_TINY_BLOB:
     487             :                         case MYSQL_TYPE_BLOB:
     488             :                         case MYSQL_TYPE_MEDIUM_BLOB:
     489             :                         case MYSQL_TYPE_LONG_BLOB:
     490             :                         case MYSQL_TYPE_TIMESTAMP:
     491             :                         case MYSQL_TYPE_DECIMAL:
     492             :                         case MYSQL_TYPE_GEOMETRY:
     493             : #ifdef FIELD_TYPE_NEWDECIMAL
     494             :                         case MYSQL_TYPE_NEWDECIMAL:
     495             : #endif
     496             :                         {
     497             : #if MYSQL_VERSION_ID >= 50107
     498             :                                 /* Changed to my_bool in MySQL 5.1. See MySQL Bug #16144 */
     499             :                                 my_bool tmp;
     500             : #else
     501             :                                 zend_ulong tmp = 0;
     502             : #endif
     503             :                                 stmt->result.buf[ofs].type = IS_STRING;
     504             :                                 /*
     505             :                                         If the user has called $stmt->store_result() then we have asked
     506             :                                         max_length to be updated. this is done only for BLOBS because we don't want to allocate
     507             :                                         big chunkgs of memory 2^16 or 2^24
     508             :                                 */
     509             :                                 if (stmt->stmt->fields[ofs].max_length == 0 &&
     510             :                                         !mysql_stmt_attr_get(stmt->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &tmp) && !tmp)
     511             :                                 {
     512             :                                         /*
     513             :                                           Allocate directly 256 because it's easier to allocate a bit more
     514             :                                           than update max length even for text columns. Try SELECT UNION SELECT UNION with
     515             :                                           different lengths and you will see that we get different lengths in stmt->stmt->fields[ofs].length
     516             :                                           The just take 256 and saves us from realloc-ing.
     517             :                                         */
     518             :                                         stmt->result.buf[ofs].buflen =
     519             :                                                 (stmt->stmt->fields) ? (stmt->stmt->fields[ofs].length) ? stmt->stmt->fields[ofs].length + 1: 256: 256;
     520             : 
     521             :                                 } else {
     522             :                                         /*
     523             :                                                 the user has called store_result(). if he does not there is no way to determine the
     524             :                                                 libmysql does not allow us to allocate 0 bytes for a buffer so we try 1
     525             :                                         */
     526             :                                         if (!(stmt->result.buf[ofs].buflen = stmt->stmt->fields[ofs].max_length))
     527             :                                                 ++stmt->result.buf[ofs].buflen;
     528             :                                 }
     529             :                                 stmt->result.buf[ofs].val = (char *)emalloc(stmt->result.buf[ofs].buflen);
     530             :                                 bind[ofs].buffer_type = MYSQL_TYPE_STRING;
     531             :                                 bind[ofs].buffer = stmt->result.buf[ofs].val;
     532             :                                 bind[ofs].is_null = &stmt->result.is_null[ofs];
     533             :                                 bind[ofs].buffer_length = stmt->result.buf[ofs].buflen;
     534             :                                 bind[ofs].length = &stmt->result.buf[ofs].output_len;
     535             :                                 break;
     536             :                         }
     537             :                         default:
     538             :                                 php_error_docref(NULL, E_WARNING, "Server returned unknown type %ld. Probably your client library is incompatible with the server version you use!", col_type);
     539             :                                 break;
     540             :                 }
     541             :         }
     542             : 
     543             :         rc = mysql_stmt_bind_result(stmt->stmt, bind);
     544             :         MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
     545             : 
     546             :         if (rc) {
     547             :                 /* dont close the statement or subsequent usage (for example ->execute()) will lead to crash */
     548             :                 for (i=0; i < var_cnt ; i++) {
     549             :                         if (stmt->result.buf[i].val) {
     550             :                                 efree(stmt->result.buf[i].val);
     551             :                         }
     552             :                 }
     553             :                 /* Don't free stmt->result.is_null because is_null & buf are one block of memory  */
     554             :                 efree(stmt->result.buf);
     555             :         } else {
     556             :                 stmt->result.var_cnt = var_cnt;
     557             :                 stmt->result.vars = safe_emalloc((var_cnt), sizeof(zval), 0);
     558             :                 for (i = 0; i < var_cnt; i++) {
     559             :                         ZVAL_COPY(&stmt->result.vars[i], &args[i]);
     560             :                 }
     561             :         }
     562             :         efree(bind);
     563             : 
     564             :         return rc;
     565             : }
     566             : #else
     567             : static int
     568        1270 : mysqli_stmt_bind_result_do_bind(MY_STMT *stmt, zval *args, unsigned int argc)
     569             : {
     570             :         unsigned int i;
     571        1270 :         MYSQLND_RESULT_BIND *params = mysqlnd_stmt_alloc_result_bind(stmt->stmt);
     572        1270 :         if (params) {
     573        6795 :                 for (i = 0; i < argc; i++) {
     574        5525 :                         ZVAL_COPY_VALUE(&params[i].zv, &args[i]);
     575             :                 }
     576        1270 :                 return mysqlnd_stmt_bind_result(stmt->stmt, params);
     577             :         }
     578           0 :         return FAIL;
     579             : }
     580             : #endif
     581             : /* }}} */
     582             : 
     583             : /* {{{ proto bool mysqli_stmt_bind_result(object stmt, mixed var [,mixed ...])
     584             :    Bind variables to a prepared statement for result storage */
     585        1278 : PHP_FUNCTION(mysqli_stmt_bind_result)
     586             : {
     587             :         zval            *args;
     588             :         int                     argc;
     589             :         zend_ulong              rc;
     590             :         MY_STMT         *stmt;
     591             :         zval            *mysql_stmt;
     592             : 
     593        1278 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O+", &mysql_stmt, mysqli_stmt_class_entry, &args, &argc) == FAILURE) {
     594           4 :                 return;
     595             :         }
     596             : 
     597        1274 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
     598             : 
     599        1273 :         if (argc != mysql_stmt_field_count(stmt->stmt)) {
     600           3 :                 php_error_docref(NULL, E_WARNING, "Number of bind variables doesn't match number of fields in prepared statement");
     601           3 :                 RETURN_FALSE;
     602             :         }
     603             : 
     604        1270 :         rc = mysqli_stmt_bind_result_do_bind(stmt, args, argc);
     605        1270 :         RETURN_BOOL(!rc);
     606             : }
     607             : /* }}} */
     608             : 
     609             : /* {{{ proto bool mysqli_change_user(object link, string user, string password, string database)
     610             :    Change logged-in user of the active connection */
     611          42 : PHP_FUNCTION(mysqli_change_user)
     612             : {
     613             :         MY_MYSQL        *mysql;
     614          42 :         zval            *mysql_link = NULL;
     615             :         char            *user, *password, *dbname;
     616             :         size_t                  user_len, password_len, dbname_len;
     617             :         zend_ulong              rc;
     618             : #if !defined(MYSQLI_USE_MYSQLND) && defined(HAVE_MYSQLI_SET_CHARSET)
     619             :         const           CHARSET_INFO * old_charset;
     620             : #endif
     621             : 
     622          42 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Osss", &mysql_link, mysqli_link_class_entry, &user, &user_len, &password, &password_len, &dbname, &dbname_len) == FAILURE) {
     623          14 :                 return;
     624             :         }
     625          28 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
     626             : 
     627             : #if !defined(MYSQLI_USE_MYSQLND) && defined(HAVE_MYSQLI_SET_CHARSET)
     628             :         old_charset = mysql->mysql->charset;
     629             : #endif
     630             : 
     631             : #if defined(MYSQLI_USE_MYSQLND)
     632          25 :         rc = mysqlnd_change_user_ex(mysql->mysql, user, password, dbname, FALSE, (size_t) password_len);
     633             : #else
     634             :         rc = mysql_change_user(mysql->mysql, user, password, dbname);
     635             : #endif
     636          25 :         MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
     637             : 
     638          25 :         if (rc) {
     639          14 :                 RETURN_FALSE;
     640             :         }
     641             : #if !defined(MYSQLI_USE_MYSQLND) && defined(HAVE_MYSQLI_SET_CHARSET)
     642             :         if (mysql_get_server_version(mysql->mysql) < 501023L) {
     643             :                 /*
     644             :                   Request the current charset, or it will be reset to the system one.
     645             :                   5.0 doesn't support it. Support added in 5.1.23 by fixing the following bug :
     646             :                   Bug #30472 libmysql doesn't reset charset, insert_id after succ. mysql_change_user() call
     647             :                 */
     648             :                 rc = mysql_set_character_set(mysql->mysql, old_charset->csname);
     649             :         }
     650             : #endif
     651             : 
     652          11 :         RETURN_TRUE;
     653             : }
     654             : /* }}} */
     655             : 
     656             : /* {{{ proto string mysqli_character_set_name(object link)
     657             :    Returns the name of the character set used for this connection */
     658          18 : PHP_FUNCTION(mysqli_character_set_name)
     659             : {
     660             :         MY_MYSQL        *mysql;
     661             :         zval            *mysql_link;
     662             :         const char      *cs_name;
     663             : 
     664          18 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
     665           5 :                 return;
     666             :         }
     667             : 
     668          13 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
     669          10 :         cs_name = mysql_character_set_name(mysql->mysql);
     670          10 :         if (cs_name) {
     671          20 :                 RETURN_STRING(cs_name);
     672             :         }
     673             : }
     674             : /* }}} */
     675             : 
     676             : /* {{{ php_mysqli_close */
     677        1248 : void php_mysqli_close(MY_MYSQL * mysql, int close_type, int resource_status)
     678             : {
     679        1248 :         if (resource_status > MYSQLI_STATUS_INITIALIZED) {
     680        1208 :                 MyG(num_links)--;
     681             :         }
     682             : 
     683        1248 :         if (!mysql->persistent) {
     684        1164 :                 mysqli_close(mysql->mysql, close_type);
     685             :         } else {
     686             :                 zend_resource *le;
     687         168 :                 if ((le = zend_hash_find_ptr(&EG(persistent_list), mysql->hash_key)) != NULL) {
     688          84 :                         if (le->type == php_le_pmysqli()) {
     689          84 :                                 mysqli_plist_entry *plist = (mysqli_plist_entry *) le->ptr;
     690             : #if defined(MYSQLI_USE_MYSQLND)
     691          84 :                                 mysqlnd_end_psession(mysql->mysql);
     692             : #endif
     693             : 
     694          86 :                                 if (MyG(rollback_on_cached_plink) &&
     695             : #if !defined(MYSQLI_USE_MYSQLND)
     696             :                                         mysqli_commit_or_rollback_libmysql(mysql->mysql, FALSE, TRANS_COR_NO_OPT, NULL))
     697             : #else
     698           2 :                                         FAIL == mysqlnd_rollback(mysql->mysql, TRANS_COR_NO_OPT, NULL))
     699             : #endif
     700             :                                 {
     701           0 :                                         mysqli_close(mysql->mysql, close_type);
     702             :                                 } else {
     703          84 :                                         zend_ptr_stack_push(&plist->free_links, mysql->mysql);
     704          84 :                                         MyG(num_inactive_persistent)++;
     705             :                                 }
     706          84 :                                 MyG(num_active_persistent)--;
     707             :                         }
     708             :                 }
     709          84 :                 mysql->persistent = FALSE;
     710             :         }
     711        1248 :         mysql->mysql = NULL;
     712             : 
     713        1248 :         php_clear_mysql(mysql);
     714        1248 : }
     715             : /* }}} */
     716             : 
     717             : /* {{{ proto bool mysqli_close(object link)
     718             :    Close connection */
     719        1059 : PHP_FUNCTION(mysqli_close)
     720             : {
     721             :         zval            *mysql_link;
     722             :         MY_MYSQL        *mysql;
     723             : 
     724        1059 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
     725           9 :                 return;
     726             :         }
     727             : 
     728        1050 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_INITIALIZED);
     729             : 
     730        1048 :         php_mysqli_close(mysql, MYSQLI_CLOSE_EXPLICIT, ((MYSQLI_RESOURCE *)(Z_MYSQLI_P(mysql_link))->ptr)->status);
     731        1048 :         ((MYSQLI_RESOURCE *)(Z_MYSQLI_P(mysql_link))->ptr)->status = MYSQLI_STATUS_UNKNOWN;
     732             : 
     733        1048 :         MYSQLI_CLEAR_RESOURCE(mysql_link);
     734        1048 :         efree(mysql);
     735        1048 :         RETURN_TRUE;
     736             : }
     737             : /* }}} */
     738             : 
     739             : /* {{{ proto bool mysqli_commit(object link[, int flags [, string name ]])
     740             :    Commit outstanding actions and close transaction */
     741          18 : PHP_FUNCTION(mysqli_commit)
     742             : {
     743             :         MY_MYSQL        *mysql;
     744             :         zval            *mysql_link;
     745          18 :         zend_long               flags = TRANS_COR_NO_OPT;
     746          18 :         char *          name = NULL;
     747          18 :         size_t                  name_len = 0;
     748             : 
     749          18 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|ls", &mysql_link, mysqli_link_class_entry, &flags, &name, &name_len) == FAILURE) {
     750           3 :                 return;
     751             :         }
     752          15 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
     753             : 
     754             : #if !defined(MYSQLI_USE_MYSQLND)
     755             :         if (mysqli_commit_or_rollback_libmysql(mysql->mysql, TRUE, flags, name)) {
     756             : #else
     757          12 :         if (FAIL == mysqlnd_commit(mysql->mysql, flags, name)) {
     758             : #endif
     759           0 :                 RETURN_FALSE;
     760             :         }
     761          12 :         RETURN_TRUE;
     762             : }
     763             : /* }}} */
     764             : 
     765             : /* {{{ proto bool mysqli_data_seek(object result, int offset)
     766             :    Move internal result pointer */
     767        1135 : PHP_FUNCTION(mysqli_data_seek)
     768             : {
     769             :         MYSQL_RES       *result;
     770             :         zval            *mysql_result;
     771             :         zend_long               offset;
     772             : 
     773        1135 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &mysql_result, mysqli_result_class_entry, &offset) == FAILURE) {
     774           7 :                 return;
     775             :         }
     776             : 
     777        1128 :         MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
     778             : 
     779        1123 :         if (mysqli_result_is_unbuffered(result)) {
     780           3 :                 php_error_docref(NULL, E_WARNING, "Function cannot be used with MYSQL_USE_RESULT");
     781           3 :                 RETURN_FALSE;
     782             :         }
     783             : 
     784        1120 :         if (offset < 0 || (uint64_t)offset >= mysql_num_rows(result)) {
     785        1020 :                 RETURN_FALSE;
     786             :         }
     787             : 
     788         100 :         mysql_data_seek(result, offset);
     789         100 :         RETURN_TRUE;
     790             : }
     791             : /* }}} */
     792             : 
     793             : /* {{{ proto void mysqli_debug(string debug)
     794             : */
     795           0 : PHP_FUNCTION(mysqli_debug)
     796             : {
     797             :         char    *debug;
     798             :         size_t          debug_len;
     799             : 
     800           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &debug, &debug_len) == FAILURE) {
     801           0 :                 return;
     802             :         }
     803             : 
     804           0 :         mysql_debug(debug);
     805           0 :         RETURN_TRUE;
     806             : }
     807             : /* }}} */
     808             : 
     809             : /* {{{ proto bool mysqli_dump_debug_info(object link)
     810             : */
     811           7 : PHP_FUNCTION(mysqli_dump_debug_info)
     812             : {
     813             :         MY_MYSQL        *mysql;
     814             :         zval            *mysql_link;
     815             : 
     816           7 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
     817           3 :                 return;
     818             :         }
     819           4 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
     820             : 
     821           2 :         RETURN_BOOL(!mysql_dump_debug_info(mysql->mysql))
     822             : }
     823             : /* }}} */
     824             : 
     825             : /* {{{ proto int mysqli_errno(object link)
     826             :    Returns the numerical value of the error message from previous MySQL operation */
     827          72 : PHP_FUNCTION(mysqli_errno)
     828             : {
     829             :         MY_MYSQL        *mysql;
     830             :         zval            *mysql_link;
     831             : 
     832          72 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
     833           4 :                 return;
     834             :         }
     835          68 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
     836          61 :         RETURN_LONG(mysql_errno(mysql->mysql));
     837             : }
     838             : /* }}} */
     839             : 
     840             : /* {{{ proto string mysqli_error(object link)
     841             :    Returns the text of the error message from previous MySQL operation */
     842          55 : PHP_FUNCTION(mysqli_error)
     843             : {
     844             :         MY_MYSQL        *mysql;
     845             :         zval            *mysql_link;
     846             :         const char      *err;
     847             : 
     848          55 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
     849           6 :                 return;
     850             :         }
     851          49 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
     852          41 :         err = mysql_error(mysql->mysql);
     853          41 :         if (err) {
     854          82 :                 RETURN_STRING(err);
     855             :         }
     856             : }
     857             : /* }}} */
     858             : 
     859             : /* {{{ proto bool mysqli_stmt_execute(object stmt)
     860             :    Execute a prepared statement */
     861        4171 : PHP_FUNCTION(mysqli_stmt_execute)
     862             : {
     863             :         MY_STMT         *stmt;
     864             :         zval            *mysql_stmt;
     865             : #ifndef MYSQLI_USE_MYSQLND
     866             :         unsigned int    i;
     867             : #endif
     868             : 
     869        4171 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
     870           2 :                 return;
     871             :         }
     872        4169 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
     873             : 
     874             : #ifndef MYSQLI_USE_MYSQLND
     875             :         if (stmt->param.var_cnt) {
     876             :                 int j;
     877             :                 for (i = 0; i < stmt->param.var_cnt; i++) {
     878             :                         if (!Z_ISREF(stmt->param.vars[i])) {
     879             :                                 continue;
     880             :                         }
     881             :                         for (j = i + 1; j < stmt->param.var_cnt; j++) {
     882             :                                 /* Oops, someone binding the same variable - clone */
     883             :                                 if (Z_TYPE(stmt->param.vars[j]) == Z_TYPE(stmt->param.vars[i]) &&
     884             :                                                 Z_REFVAL(stmt->param.vars[j]) == Z_REFVAL(stmt->param.vars[i])) {
     885             :                                         SEPARATE_ZVAL(&stmt->param.vars[j]);
     886             :                                         break;
     887             :                                 }
     888             :                         }
     889             :                 }
     890             :         }
     891             :         for (i = 0; i < stmt->param.var_cnt; i++) {
     892             :                 if (!Z_ISUNDEF(stmt->param.vars[i])) {
     893             :                         zval *param;
     894             :                         if (Z_ISREF(stmt->param.vars[i])) {
     895             :                                 param = Z_REFVAL(stmt->param.vars[i]);
     896             :                         } else {
     897             :                                 param = &stmt->param.vars[i];
     898             :                         }
     899             :                         if (!(stmt->param.is_null[i] = (Z_ISNULL_P(param)))) {
     900             :                                 switch (stmt->stmt->params[i].buffer_type) {
     901             :                                         case MYSQL_TYPE_VAR_STRING:
     902             :                                                 convert_to_string_ex(param);
     903             :                                                 stmt->stmt->params[i].buffer = Z_STRVAL_P(param);
     904             :                                                 stmt->stmt->params[i].buffer_length = Z_STRLEN_P(param);
     905             :                                                 break;
     906             :                                         case MYSQL_TYPE_DOUBLE:
     907             :                                                 convert_to_double_ex(param);
     908             :                                                 stmt->stmt->params[i].buffer = &Z_DVAL_P(param);
     909             :                                                 break;
     910             :                                         case MYSQL_TYPE_LONGLONG:
     911             :                                         case MYSQL_TYPE_LONG:
     912             :                                                 convert_to_long_ex(param);
     913             :                                                 stmt->stmt->params[i].buffer = &Z_LVAL_P(param);
     914             :                                                 break;
     915             :                                         default:
     916             :                                                 break;
     917             :                                 }
     918             :                         }
     919             :                 }
     920             :         }
     921             : #endif
     922             : 
     923        4166 :         if (mysql_stmt_execute(stmt->stmt)) {
     924          10 :                 MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
     925          10 :                 RETVAL_FALSE;
     926             :         } else {
     927        4156 :                 RETVAL_TRUE;
     928             :         }
     929             : 
     930        4166 :         if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
     931           0 :                 php_mysqli_report_index(stmt->query, mysqli_stmt_server_status(stmt->stmt));
     932             :         }
     933             : }
     934             : /* }}} */
     935             : 
     936             : #ifndef MYSQLI_USE_MYSQLND
     937             : /* {{{ void mysqli_stmt_fetch_libmysql
     938             :    Fetch results from a prepared statement into the bound variables */
     939             : void mysqli_stmt_fetch_libmysql(INTERNAL_FUNCTION_PARAMETERS)
     940             : {
     941             :         MY_STMT         *stmt;
     942             :         zval                    *mysql_stmt;
     943             :         unsigned int    i;
     944             :         zend_ulong                      ret;
     945             :         unsigned int    uval;
     946             :         my_ulonglong    llval;
     947             : 
     948             : 
     949             :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
     950             :                 return;
     951             :         }
     952             :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
     953             : 
     954             :         /* reset buffers */
     955             :         for (i = 0; i < stmt->result.var_cnt; i++) {
     956             :                 if (stmt->result.buf[i].type == IS_STRING) {
     957             :                         memset(stmt->result.buf[i].val, 0, stmt->result.buf[i].buflen);
     958             :                 }
     959             :         }
     960             :         ret = mysql_stmt_fetch(stmt->stmt);
     961             : #ifdef MYSQL_DATA_TRUNCATED
     962             :         if (!ret || ret == MYSQL_DATA_TRUNCATED) {
     963             : #else
     964             :         if (!ret) {
     965             : #endif
     966             :                 for (i = 0; i < stmt->result.var_cnt; i++) {
     967             :                         zval *result;
     968             :                         /* it must be a reference, isn't it? */
     969             :                         if (Z_ISREF(stmt->result.vars[i])) {
     970             :                                 result = Z_REFVAL(stmt->result.vars[i]);
     971             :                         } else {
     972             :                                 result = &stmt->result.vars[i];
     973             :                         }
     974             :                         /*
     975             :                           QQ: Isn't it quite better to call zval_dtor(). What if the user has
     976             :                           assigned a resource, or an array to the bound variable? We are going
     977             :                           to leak probably. zval_dtor() will handle also Unicode/Non-unicode mode.
     978             :                         */
     979             :                         /* Even if the string is of length zero there is one byte alloced so efree() in all cases */
     980             :                         zval_ptr_dtor(result);
     981             :                         if (!stmt->result.is_null[i]) {
     982             :                                 switch (stmt->result.buf[i].type) {
     983             :                                         case IS_LONG:
     984             :                                                 if ((stmt->stmt->fields[i].type == MYSQL_TYPE_LONG)
     985             :                                                     && (stmt->stmt->fields[i].flags & UNSIGNED_FLAG))
     986             :                                                 {
     987             :                                                         /* unsigned int (11) */
     988             :                                                         uval= *(unsigned int *) stmt->result.buf[i].val;
     989             : #if SIZEOF_ZEND_LONG==4
     990             :                                                         if (uval > INT_MAX) {
     991             :                                                                 char *tmp, *p;
     992             :                                                                 int j = 10;
     993             :                                                                 tmp = emalloc(11);
     994             :                                                                 p= &tmp[9];
     995             :                                                                 do {
     996             :                                                                         *p-- = (uval % 10) + 48;
     997             :                                                                         uval = uval / 10;
     998             :                                                                 } while (--j > 0);
     999             :                                                                 tmp[10]= '\0';
    1000             :                                                                 /* unsigned int > INT_MAX is 10 digits - ALWAYS */
    1001             :                                                                 ZVAL_STRINGL(result, tmp, 10);
    1002             :                                                                 efree(tmp);
    1003             :                                                                 break;
    1004             :                                                         }
    1005             : #endif
    1006             :                                                 }
    1007             :                                                 if (stmt->stmt->fields[i].flags & UNSIGNED_FLAG) {
    1008             :                                                         ZVAL_LONG(result, *(unsigned int *)stmt->result.buf[i].val);
    1009             :                                                 } else {
    1010             :                                                         ZVAL_LONG(result, *(int *)stmt->result.buf[i].val);
    1011             :                                                 }
    1012             :                                                 break;
    1013             :                                         case IS_DOUBLE:
    1014             :                                         {
    1015             :                                                 double dval;
    1016             :                                                 if (stmt->stmt->bind[i].buffer_type == MYSQL_TYPE_FLOAT) {
    1017             : #ifndef NOT_FIXED_DEC
    1018             : # define NOT_FIXED_DEC 31
    1019             : #endif
    1020             :                                                         dval = mysql_float_to_double(*(float *)stmt->result.buf[i].val,
    1021             :                                                                                 (stmt->stmt->fields[i].decimals >= NOT_FIXED_DEC) ? -1 :
    1022             :                                                                                 stmt->stmt->fields[i].decimals);
    1023             :                                                 } else {
    1024             :                                                         dval = *((double *)stmt->result.buf[i].val);
    1025             :                                                 }
    1026             : 
    1027             :                                                 ZVAL_DOUBLE(result, dval);
    1028             :                                                 break;
    1029             :                                         }
    1030             :                                         case IS_STRING:
    1031             :                                                 if (stmt->stmt->bind[i].buffer_type == MYSQL_TYPE_LONGLONG
    1032             : #if MYSQL_VERSION_ID > 50002
    1033             :                                                  || stmt->stmt->bind[i].buffer_type == MYSQL_TYPE_BIT
    1034             : #endif
    1035             :                                                  ) {
    1036             :                                                         my_bool uns = (stmt->stmt->fields[i].flags & UNSIGNED_FLAG)? 1:0;
    1037             : #if MYSQL_VERSION_ID > 50002
    1038             :                                                         if (stmt->stmt->bind[i].buffer_type == MYSQL_TYPE_BIT) {
    1039             :                                                                 switch (stmt->result.buf[i].output_len) {
    1040             :                                                                         case 8:llval = (my_ulonglong)  bit_uint8korr(stmt->result.buf[i].val);break;
    1041             :                                                                         case 7:llval = (my_ulonglong)  bit_uint7korr(stmt->result.buf[i].val);break;
    1042             :                                                                         case 6:llval = (my_ulonglong)  bit_uint6korr(stmt->result.buf[i].val);break;
    1043             :                                                                         case 5:llval = (my_ulonglong)  bit_uint5korr(stmt->result.buf[i].val);break;
    1044             :                                                                         case 4:llval = (my_ulonglong)  bit_uint4korr(stmt->result.buf[i].val);break;
    1045             :                                                                         case 3:llval = (my_ulonglong)  bit_uint3korr(stmt->result.buf[i].val);break;
    1046             :                                                                         case 2:llval = (my_ulonglong)  bit_uint2korr(stmt->result.buf[i].val);break;
    1047             :                                                                         case 1:llval = (my_ulonglong)  uint1korr(stmt->result.buf[i].val);break;
    1048             :                                                                 }
    1049             :                                                         } else
    1050             : #endif
    1051             :                                                         {
    1052             :                                                                 llval= *(my_ulonglong *) stmt->result.buf[i].val;
    1053             :                                                         }
    1054             : #if SIZEOF_ZEND_LONG==8
    1055             :                                                         if (uns && llval > 9223372036854775807L) {
    1056             : #elif SIZEOF_ZEND_LONG==4
    1057             :                                                         if ((uns && llval > L64(2147483647)) ||
    1058             :                                                                 (!uns && (( L64(2147483647) < (my_longlong) llval) ||
    1059             :                                                                 (L64(-2147483648) > (my_longlong) llval))))
    1060             :                                                         {
    1061             : #endif
    1062             :                                                                 char tmp[22];
    1063             :                                                                 /* even though lval is declared as unsigned, the value
    1064             :                                                                  * may be negative. Therefor we cannot use MYSQLI_LLU_SPEC and must
    1065             :                                                                  * use MYSQLI_LL_SPEC.
    1066             :                                                                  */
    1067             :                                                                 snprintf(tmp, sizeof(tmp), (stmt->stmt->fields[i].flags & UNSIGNED_FLAG)? MYSQLI_LLU_SPEC : MYSQLI_LL_SPEC, llval);
    1068             :                                                                 ZVAL_STRING(result, tmp);
    1069             :                                                         } else {
    1070             :                                                                 ZVAL_LONG(result, llval);
    1071             :                                                         }
    1072             :                                                 } else {
    1073             : #if defined(MYSQL_DATA_TRUNCATED) && MYSQL_VERSION_ID > 50002
    1074             :                                                         if (ret == MYSQL_DATA_TRUNCATED && *(stmt->stmt->bind[i].error) != 0) {
    1075             :                                                                 /* result was truncated */
    1076             :                                                                 ZVAL_STRINGL(result, stmt->result.buf[i].val, stmt->stmt->bind[i].buffer_length);
    1077             :                                                         } else {
    1078             : #else
    1079             :                                                         {
    1080             : #endif
    1081             :                                                                 ZVAL_STRINGL(result, stmt->result.buf[i].val, stmt->result.buf[i].output_len);
    1082             :                                                         }
    1083             :                                                 }
    1084             :                                                 break;
    1085             :                                         default:
    1086             :                                                 break;
    1087             :                                 }
    1088             :                         } else {
    1089             :                                 ZVAL_NULL(result);
    1090             :                         }
    1091             :                 }
    1092             :         } else {
    1093             :                 MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
    1094             :         }
    1095             : 
    1096             :         switch (ret) {
    1097             :                 case 0:
    1098             : #ifdef MYSQL_DATA_TRUNCATED
    1099             :                 /* according to SQL standard truncation (e.g. loss of precision is
    1100             :                    not an error) - for detecting possible truncation you have to
    1101             :                    check mysqli_stmt_warning
    1102             :                 */
    1103             :                 case MYSQL_DATA_TRUNCATED:
    1104             : #endif
    1105             :                         RETURN_TRUE;
    1106             :                 break;
    1107             :                 case 1:
    1108             :                         RETURN_FALSE;
    1109             :                 break;
    1110             :                 default:
    1111             :                         RETURN_NULL();
    1112             :                 break;
    1113             :         }
    1114             : }
    1115             : /* }}} */
    1116             : #else
    1117             : /* {{{ mixed mysqli_stmt_fetch_mysqlnd */
    1118       19037 : void mysqli_stmt_fetch_mysqlnd(INTERNAL_FUNCTION_PARAMETERS)
    1119             : {
    1120             :         MY_STMT         *stmt;
    1121             :         zval            *mysql_stmt;
    1122             :         zend_bool       fetched_anything;
    1123             : 
    1124       19037 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
    1125           3 :                 return;
    1126             :         }
    1127       19034 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
    1128             : 
    1129       19029 :         if (FAIL  == mysqlnd_stmt_fetch(stmt->stmt, &fetched_anything)) {
    1130           8 :                 RETURN_BOOL(FALSE);
    1131       19021 :         } else if (fetched_anything == TRUE) {
    1132       18865 :                 RETURN_BOOL(TRUE);
    1133             :         } else {
    1134         156 :                 RETURN_NULL();
    1135             :         }
    1136             : }
    1137             : #endif
    1138             : /* }}} */
    1139             : 
    1140             : /* {{{ proto mixed mysqli_stmt_fetch(object stmt)
    1141             :    Fetch results from a prepared statement into the bound variables */
    1142       19037 : PHP_FUNCTION(mysqli_stmt_fetch)
    1143             : {
    1144             : #if !defined(MYSQLI_USE_MYSQLND)
    1145             :         mysqli_stmt_fetch_libmysql(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1146             : #else
    1147       19037 :         mysqli_stmt_fetch_mysqlnd(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1148             : #endif
    1149       19037 : }
    1150             : /* }}} */
    1151             : 
    1152             : /* {{{  php_add_field_properties */
    1153        1271 : static void php_add_field_properties(zval *value, const MYSQL_FIELD *field)
    1154             : {
    1155             : #ifdef MYSQLI_USE_MYSQLND
    1156        2542 :         add_property_str(value, "name", zend_string_copy(field->sname));
    1157             : #else
    1158             :         add_property_stringl(value, "name",(field->name ? field->name : ""), field->name_length);
    1159             : #endif
    1160             : 
    1161        1271 :         add_property_stringl(value, "orgname", (field->org_name ? field->org_name : ""), field->org_name_length);
    1162        1271 :         add_property_stringl(value, "table", (field->table ? field->table : ""), field->table_length);
    1163        1271 :         add_property_stringl(value, "orgtable", (field->org_table ? field->org_table : ""), field->org_table_length);
    1164        1271 :         add_property_stringl(value, "def", (field->def ? field->def : ""), field->def_length);
    1165        1271 :         add_property_stringl(value, "db", (field->db ? field->db : ""), field->db_length);
    1166             : 
    1167             :         /* FIXME: manually set the catalog to "def" due to bug in
    1168             :          * libmysqlclient which does not initialize field->catalog
    1169             :          * and in addition, the catalog is always be "def"
    1170             :          */
    1171        1271 :         add_property_string(value, "catalog", "def");
    1172             : 
    1173        1271 :         add_property_long(value, "max_length", field->max_length);
    1174        1271 :         add_property_long(value, "length", field->length);
    1175        1271 :         add_property_long(value, "charsetnr", field->charsetnr);
    1176        1271 :         add_property_long(value, "flags", field->flags);
    1177        1271 :         add_property_long(value, "type", field->type);
    1178        1271 :         add_property_long(value, "decimals", field->decimals);
    1179        1271 : }
    1180             : /* }}} */
    1181             : 
    1182             : /* {{{ proto mixed mysqli_fetch_field (object result)
    1183             :    Get column information from a result and return as an object */
    1184         209 : PHP_FUNCTION(mysqli_fetch_field)
    1185             : {
    1186             :         MYSQL_RES       *result;
    1187             :         zval            *mysql_result;
    1188             :         const MYSQL_FIELD       *field;
    1189             : 
    1190         209 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
    1191           3 :                 return;
    1192             :         }
    1193             : 
    1194         206 :         MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
    1195             : 
    1196         200 :         if (!(field = mysql_fetch_field(result))) {
    1197           9 :                 RETURN_FALSE;
    1198             :         }
    1199             : 
    1200         191 :         object_init(return_value);
    1201         191 :         php_add_field_properties(return_value, field);
    1202             : }
    1203             : /* }}} */
    1204             : 
    1205             : /* {{{ proto mixed mysqli_fetch_fields (object result)
    1206             :    Return array of objects containing field meta-data */
    1207         516 : PHP_FUNCTION(mysqli_fetch_fields)
    1208             : {
    1209             :         MYSQL_RES       *result;
    1210             :         zval            *mysql_result;
    1211             :         zval            obj;
    1212             : 
    1213             :         unsigned int i;
    1214             : 
    1215         516 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
    1216           2 :                 return;
    1217             :         }
    1218             : 
    1219         514 :         MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
    1220             : 
    1221         513 :         array_init(return_value);
    1222             : 
    1223        1555 :         for (i = 0; i < mysql_num_fields(result); i++) {
    1224        1042 :                 const MYSQL_FIELD *field = mysql_fetch_field_direct(result, i);
    1225             : 
    1226        1042 :                 object_init(&obj);
    1227             : 
    1228        1042 :                 php_add_field_properties(&obj, field);
    1229        1042 :                 add_index_zval(return_value, i, &obj);
    1230             :         }
    1231             : }
    1232             : /* }}} */
    1233             : 
    1234             : /* {{{ proto mixed mysqli_fetch_field_direct (object result, int offset)
    1235             :    Fetch meta-data for a single field */
    1236          51 : PHP_FUNCTION(mysqli_fetch_field_direct)
    1237             : {
    1238             :         MYSQL_RES       *result;
    1239             :         zval            *mysql_result;
    1240             :         const MYSQL_FIELD       *field;
    1241             :         zend_long               offset;
    1242             : 
    1243          51 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &mysql_result, mysqli_result_class_entry, &offset) == FAILURE) {
    1244           7 :                 return;
    1245             :         }
    1246             : 
    1247          44 :         MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
    1248             : 
    1249          42 :         if (offset < 0 || offset >= (zend_long) mysql_num_fields(result)) {
    1250           4 :                 php_error_docref(NULL, E_WARNING, "Field offset is invalid for resultset");
    1251           4 :                 RETURN_FALSE;
    1252             :         }
    1253             : 
    1254          38 :         if (!(field = mysql_fetch_field_direct(result,offset))) {
    1255           0 :                 RETURN_FALSE;
    1256             :         }
    1257             : 
    1258          38 :         object_init(return_value);
    1259          38 :         php_add_field_properties(return_value, field);
    1260             : }
    1261             : /* }}} */
    1262             : 
    1263             : /* {{{ proto mixed mysqli_fetch_lengths (object result)
    1264             :    Get the length of each output in a result */
    1265          19 : PHP_FUNCTION(mysqli_fetch_lengths)
    1266             : {
    1267             :         MYSQL_RES               *result;
    1268             :         zval                    *mysql_result;
    1269             :         unsigned int    i;
    1270             :         zend_ulong      *ret;
    1271             : 
    1272          19 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
    1273           2 :                 return;
    1274             :         }
    1275             : 
    1276          17 :         MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
    1277             : 
    1278          16 :         if (!(ret = mysql_fetch_lengths(result))) {
    1279           7 :                 RETURN_FALSE;
    1280             :         }
    1281             : 
    1282           9 :         array_init(return_value);
    1283             : 
    1284          27 :         for (i = 0; i < mysql_num_fields(result); i++) {
    1285          18 :                 add_index_long(return_value, i, ret[i]);
    1286             :         }
    1287             : }
    1288             : /* }}} */
    1289             : 
    1290             : /* {{{ proto array mysqli_fetch_row (object result)
    1291             :    Get a result row as an enumerated array */
    1292          58 : PHP_FUNCTION(mysqli_fetch_row)
    1293             : {
    1294          58 :         php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQLI_NUM, 0);
    1295          58 : }
    1296             : /* }}} */
    1297             : 
    1298             : /* {{{ proto int mysqli_field_count(object link)
    1299             :    Fetch the number of fields returned by the last query for the given link
    1300             : */
    1301          24 : PHP_FUNCTION(mysqli_field_count)
    1302             : {
    1303             :         MY_MYSQL        *mysql;
    1304             :         zval            *mysql_link;
    1305             : 
    1306          24 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
    1307           2 :                 return;
    1308             :         }
    1309          22 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    1310             : 
    1311          15 :         RETURN_LONG(mysql_field_count(mysql->mysql));
    1312             : }
    1313             : /* }}} */
    1314             : 
    1315             : /* {{{ proto int mysqli_field_seek(object result, int fieldnr)
    1316             :    Set result pointer to a specified field offset
    1317             : */
    1318         215 : PHP_FUNCTION(mysqli_field_seek)
    1319             : {
    1320             :         MYSQL_RES               *result;
    1321             :         zval                    *mysql_result;
    1322             :         zend_long       fieldnr;
    1323             : 
    1324         215 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &mysql_result, mysqli_result_class_entry, &fieldnr) == FAILURE) {
    1325           5 :                 return;
    1326             :         }
    1327         210 :         MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
    1328             : 
    1329         209 :         if (fieldnr < 0 || fieldnr >= mysql_num_fields(result)) {
    1330          62 :                 php_error_docref(NULL, E_WARNING, "Invalid field offset");
    1331          62 :                 RETURN_FALSE;
    1332             :         }
    1333             : 
    1334         147 :         mysql_field_seek(result, fieldnr);
    1335         147 :         RETURN_TRUE;
    1336             : }
    1337             : /* }}} */
    1338             : 
    1339             : /* {{{ proto int mysqli_field_tell(object result)
    1340             :    Get current field offset of result pointer */
    1341         156 : PHP_FUNCTION(mysqli_field_tell)
    1342             : {
    1343             :         MYSQL_RES       *result;
    1344             :         zval            *mysql_result;
    1345             : 
    1346         156 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
    1347           3 :                 return;
    1348             :         }
    1349         153 :         MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
    1350             : 
    1351         152 :         RETURN_LONG(mysql_field_tell(result));
    1352             : }
    1353             : /* }}} */
    1354             : 
    1355             : /* {{{ proto void mysqli_free_result(object result)
    1356             :    Free query result memory for the given result handle */
    1357        1266 : PHP_FUNCTION(mysqli_free_result)
    1358             : {
    1359             :         MYSQL_RES       *result;
    1360             :         zval            *mysql_result;
    1361             : 
    1362        1266 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
    1363           6 :                 return;
    1364             :         }
    1365        1260 :         MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
    1366             : 
    1367        1259 :         mysqli_free_result(result, FALSE);
    1368        1259 :         MYSQLI_CLEAR_RESOURCE(mysql_result);
    1369             : }
    1370             : /* }}} */
    1371             : 
    1372             : /* {{{ proto string mysqli_get_client_info(void)
    1373             :    Get MySQL client info */
    1374        2745 : PHP_FUNCTION(mysqli_get_client_info)
    1375             : {
    1376        2745 :         const char * info = mysql_get_client_info();
    1377        2745 :         if (info) {
    1378        5490 :                 RETURN_STRING(info);
    1379             :         }
    1380             : }
    1381             : /* }}} */
    1382             : 
    1383             : /* {{{ proto int mysqli_get_client_version(void)
    1384             :    Get MySQL client info */
    1385          18 : PHP_FUNCTION(mysqli_get_client_version)
    1386             : {
    1387          18 :         RETURN_LONG((zend_long)mysql_get_client_version());
    1388             : }
    1389             : /* }}} */
    1390             : 
    1391             : /* {{{ proto string mysqli_get_host_info (object link)
    1392             :    Get MySQL host info */
    1393          13 : PHP_FUNCTION(mysqli_get_host_info)
    1394             : {
    1395             :         MY_MYSQL        *mysql;
    1396          13 :         zval            *mysql_link = NULL;
    1397             : 
    1398          13 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
    1399           2 :                 return;
    1400             :         }
    1401          11 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    1402             : #if !defined(MYSQLI_USE_MYSQLND)
    1403             :         RETURN_STRING((mysql->mysql->host_info) ? mysql->mysql->host_info : "");
    1404             : #else
    1405          10 :         RETURN_STRING((mysql->mysql->data->host_info) ? mysql->mysql->data->host_info : "");
    1406             : #endif
    1407             : }
    1408             : /* }}} */
    1409             : 
    1410             : /* {{{ proto int mysqli_get_proto_info(object link)
    1411             :    Get MySQL protocol information */
    1412          14 : PHP_FUNCTION(mysqli_get_proto_info)
    1413             : {
    1414             :         MY_MYSQL        *mysql;
    1415          14 :         zval            *mysql_link = NULL;
    1416             : 
    1417          14 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
    1418           3 :                 return;
    1419             :         }
    1420          11 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    1421           5 :         RETURN_LONG(mysql_get_proto_info(mysql->mysql));
    1422             : }
    1423             : /* }}} */
    1424             : 
    1425             : /* {{{ proto string mysqli_get_server_info(object link)
    1426             :    Get MySQL server info */
    1427          14 : PHP_FUNCTION(mysqli_get_server_info)
    1428             : {
    1429             :         MY_MYSQL        *mysql;
    1430          14 :         zval            *mysql_link = NULL;
    1431             :         const char      *info;
    1432             : 
    1433          14 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
    1434           3 :                 return;
    1435             :         }
    1436          11 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    1437             : 
    1438           5 :         info = mysql_get_server_info(mysql->mysql);
    1439           5 :         if (info) {
    1440          10 :                 RETURN_STRING(info);
    1441             :         }
    1442             : }
    1443             : /* }}} */
    1444             : 
    1445             : /* {{{ proto int mysqli_get_server_version(object link)
    1446             :    Return the MySQL version for the server referenced by the given link */
    1447          64 : PHP_FUNCTION(mysqli_get_server_version)
    1448             : {
    1449             :         MY_MYSQL        *mysql;
    1450          64 :         zval            *mysql_link = NULL;
    1451             : 
    1452          64 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
    1453           2 :                 return;
    1454             :         }
    1455          62 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    1456             : 
    1457          56 :         RETURN_LONG(mysql_get_server_version(mysql->mysql));
    1458             : }
    1459             : /* }}} */
    1460             : 
    1461             : /* {{{ proto string mysqli_info(object link)
    1462             :    Get information about the most recent query */
    1463          19 : PHP_FUNCTION(mysqli_info)
    1464             : {
    1465             :         MY_MYSQL        *mysql;
    1466          19 :         zval            *mysql_link = NULL;
    1467             :         const char      *info;
    1468             : 
    1469          19 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
    1470           2 :                 return;
    1471             :         }
    1472          17 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    1473             : 
    1474          11 :         info = mysql_info(mysql->mysql);
    1475          11 :         if (info) {
    1476          12 :                 RETURN_STRING(info);
    1477             :         }
    1478             : }
    1479             : /* }}} */
    1480             : 
    1481             : /* {{{ php_mysqli_init() */
    1482        1185 : void php_mysqli_init(INTERNAL_FUNCTION_PARAMETERS)
    1483             : {
    1484             :         MYSQLI_RESOURCE *mysqli_resource;
    1485             :         MY_MYSQL *mysql;
    1486             : 
    1487             : // TODO: We can't properly check if this was to mysql_init() in a class method
    1488             : //       or a call to mysqli->init().
    1489             : //       To solve the problem, we added instanceof check for the class of $this
    1490             : //       ???
    1491        1305 :         if (getThis() &&
    1492          60 :             instanceof_function(Z_OBJCE_P(getThis()), mysqli_link_class_entry) &&
    1493          60 :             (Z_MYSQLI_P(getThis()))->ptr) {
    1494             : //???   if (getThis() && (Z_MYSQLI_P(getThis()))->ptr) {
    1495           2 :                 return;
    1496             :         }
    1497             : 
    1498        1183 :         mysql = (MY_MYSQL *)ecalloc(1, sizeof(MY_MYSQL));
    1499             : 
    1500             : #if !defined(MYSQLI_USE_MYSQLND)
    1501             :         if (!(mysql->mysql = mysql_init(NULL)))
    1502             : #else
    1503             :         /*
    1504             :           We create always persistent, as if the user want to connecto
    1505             :           to p:somehost, we can't convert the handle then
    1506             :         */
    1507        1183 :         if (!(mysql->mysql = mysqlnd_init(MYSQLND_CLIENT_KNOWS_RSET_COPY_DATA, TRUE)))
    1508             : #endif
    1509             :         {
    1510           0 :                 efree(mysql);
    1511           0 :                 RETURN_FALSE;
    1512             :         }
    1513             : 
    1514        1183 :         mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
    1515        1183 :         mysqli_resource->ptr = (void *)mysql;
    1516        1183 :         mysqli_resource->status = MYSQLI_STATUS_INITIALIZED;
    1517             : 
    1518        2308 :         if (!getThis() || !instanceof_function(Z_OBJCE_P(getThis()), mysqli_link_class_entry)) {
    1519        1125 :                 MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_link_class_entry);
    1520             :         } else {
    1521          58 :                 (Z_MYSQLI_P(getThis()))->ptr = mysqli_resource;
    1522             :         }
    1523             : }
    1524             : /* }}} */
    1525             : 
    1526             : /* {{{ proto resource mysqli_init(void)
    1527             :    Initialize mysqli and return a resource for use with mysql_real_connect */
    1528        1169 : PHP_FUNCTION(mysqli_init)
    1529             : {
    1530        1169 :         php_mysqli_init(INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1531        1169 : }
    1532             : /* }}} */
    1533             : 
    1534             : /* {{{ proto mixed mysqli_insert_id(object link)
    1535             :    Get the ID generated from the previous INSERT operation */
    1536          28 : PHP_FUNCTION(mysqli_insert_id)
    1537             : {
    1538             :         MY_MYSQL                *mysql;
    1539             :         my_ulonglong    rc;
    1540             :         zval                    *mysql_link;
    1541             : 
    1542          28 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
    1543           3 :                 return;
    1544             :         }
    1545          25 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    1546          18 :         rc = mysql_insert_id(mysql->mysql);
    1547          18 :         MYSQLI_RETURN_LONG_INT(rc)
    1548             : }
    1549             : /* }}} */
    1550             : 
    1551             : /* {{{ proto bool mysqli_kill(object link, int processid)
    1552             :    Kill a mysql process on the server */
    1553          26 : PHP_FUNCTION(mysqli_kill)
    1554             : {
    1555             :         MY_MYSQL        *mysql;
    1556             :         zval            *mysql_link;
    1557             :         zend_long               processid;
    1558             : 
    1559          26 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &mysql_link, mysqli_link_class_entry, &processid) == FAILURE) {
    1560           2 :                 return;
    1561             :         }
    1562          24 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    1563             : 
    1564          24 :         if (processid <= 0) {
    1565           7 :                 php_error_docref(NULL, E_WARNING, "processid should have positive value");
    1566           7 :                 RETURN_FALSE;
    1567             :         }
    1568             : 
    1569          17 :         if (mysql_kill(mysql->mysql, processid)) {
    1570           3 :                 MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
    1571           3 :                 RETURN_FALSE;
    1572             :         }
    1573          14 :         RETURN_TRUE;
    1574             : }
    1575             : /* }}} */
    1576             : 
    1577             : /* {{{ proto bool mysqli_more_results(object link)
    1578             :    check if there any more query results from a multi query */
    1579          37 : PHP_FUNCTION(mysqli_more_results)
    1580             : {
    1581             :         MY_MYSQL        *mysql;
    1582             :         zval            *mysql_link;
    1583             : 
    1584          37 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
    1585           2 :                 return;
    1586             :         }
    1587          35 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    1588             : 
    1589          34 :         RETURN_BOOL(mysql_more_results(mysql->mysql));
    1590             : }
    1591             : /* }}} */
    1592             : 
    1593             : /* {{{ proto bool mysqli_next_result(object link)
    1594             :    read next result from multi_query */
    1595          44 : PHP_FUNCTION(mysqli_next_result) {
    1596             :         MY_MYSQL        *mysql;
    1597             :         zval            *mysql_link;
    1598             : 
    1599          44 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
    1600           2 :                 return;
    1601             :         }
    1602          42 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    1603             : 
    1604          41 :         if (!mysql_more_results(mysql->mysql)) {
    1605          12 :                 php_error_docref(NULL, E_STRICT, "There is no next result set. "
    1606             :                                                 "Please, call mysqli_more_results()/mysqli::more_results() to check "
    1607             :                                                 "whether to call this function/method");
    1608             :         }
    1609             : 
    1610          41 :         RETURN_BOOL(!mysql_next_result(mysql->mysql));
    1611             : }
    1612             : /* }}} */
    1613             : 
    1614             : #if defined(HAVE_STMT_NEXT_RESULT) && defined(MYSQLI_USE_MYSQLND)
    1615             : /* {{{ proto bool mysqli_stmt_next_result(object link)
    1616             :    check if there any more query results from a multi query */
    1617           3 : PHP_FUNCTION(mysqli_stmt_more_results)
    1618             : {
    1619             :         MY_STMT         *stmt;
    1620             :         zval            *mysql_stmt;
    1621             : 
    1622           3 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
    1623           0 :                 return;
    1624             :         }
    1625           3 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
    1626             : 
    1627           3 :         RETURN_BOOL(mysqlnd_stmt_more_results(stmt->stmt));
    1628             : }
    1629             : /* }}} */
    1630             : 
    1631             : /* {{{ proto bool mysqli_stmt_next_result(object link)
    1632             :    read next result from multi_query */
    1633           2 : PHP_FUNCTION(mysqli_stmt_next_result) {
    1634             :         MY_STMT         *stmt;
    1635             :         zval            *mysql_stmt;
    1636             : 
    1637           2 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
    1638           0 :                 return;
    1639             :         }
    1640           2 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
    1641             : 
    1642           2 :         if (!mysqlnd_stmt_more_results(stmt->stmt)) {
    1643           0 :                 php_error_docref(NULL, E_STRICT, "There is no next result set. "
    1644             :                                                 "Please, call mysqli_stmt_more_results()/mysqli_stmt::more_results() to check "
    1645             :                                                 "whether to call this function/method");
    1646             :         }
    1647             : 
    1648           2 :         RETURN_BOOL(!mysql_stmt_next_result(stmt->stmt));
    1649             : }
    1650             : /* }}} */
    1651             : #endif
    1652             : 
    1653             : /* {{{ proto int mysqli_num_fields(object result)
    1654             :    Get number of fields in result */
    1655          12 : PHP_FUNCTION(mysqli_num_fields)
    1656             : {
    1657             :         MYSQL_RES       *result;
    1658             :         zval            *mysql_result;
    1659             : 
    1660          12 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
    1661           2 :                 return;
    1662             :         }
    1663          10 :         MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
    1664             : 
    1665           9 :         RETURN_LONG(mysql_num_fields(result));
    1666             : }
    1667             : /* }}} */
    1668             : 
    1669             : /* {{{ proto mixed mysqli_num_rows(object result)
    1670             :    Get number of rows in result */
    1671          29 : PHP_FUNCTION(mysqli_num_rows)
    1672             : {
    1673             :         MYSQL_RES       *result;
    1674             :         zval            *mysql_result;
    1675             : 
    1676          29 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
    1677           3 :                 return;
    1678             :         }
    1679          26 :         MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
    1680             : 
    1681          25 :         if (mysqli_result_is_unbuffered_and_not_everything_is_fetched(result)) {
    1682           3 :                 php_error_docref(NULL, E_WARNING, "Function cannot be used with MYSQL_USE_RESULT");
    1683           3 :                 RETURN_LONG(0);
    1684             :         }
    1685             : 
    1686          22 :         MYSQLI_RETURN_LONG_INT(mysql_num_rows(result));
    1687             : }
    1688             : /* }}} */
    1689             : 
    1690             : /* {{{ mysqli_options_get_option_zval_type */
    1691          88 : static int mysqli_options_get_option_zval_type(int option)
    1692             : {
    1693          88 :         switch (option) {
    1694             : #ifdef MYSQLI_USE_MYSQLND
    1695             : #if PHP_MAJOR_VERSION == 6
    1696             :                 /* PHP-7 doesn't supprt unicode yet ??? */
    1697             :                 case MYSQLND_OPT_NUMERIC_AND_DATETIME_AS_UNICODE:
    1698             : #endif
    1699             :                 case MYSQLND_OPT_NET_CMD_BUFFER_SIZE:
    1700             :                 case MYSQLND_OPT_NET_READ_BUFFER_SIZE:
    1701             : #ifdef MYSQLND_STRING_TO_INT_CONVERSION
    1702             :                 case MYSQLND_OPT_INT_AND_FLOAT_NATIVE:
    1703             : #endif
    1704             : #endif /* MYSQLI_USE_MYSQLND */
    1705             :                 case MYSQL_OPT_CONNECT_TIMEOUT:
    1706             : #ifdef MYSQL_REPORT_DATA_TRUNCATION
    1707             :                 case MYSQL_REPORT_DATA_TRUNCATION:
    1708             : #endif
    1709             :                 case MYSQL_OPT_LOCAL_INFILE:
    1710             :                 case MYSQL_OPT_NAMED_PIPE:
    1711             : #ifdef MYSQL_OPT_PROTOCOL
    1712             :                 case MYSQL_OPT_PROTOCOL:
    1713             : #endif /* MySQL 4.1.0 */
    1714             : #ifdef MYSQL_OPT_READ_TIMEOUT
    1715             :                 case MYSQL_OPT_READ_TIMEOUT:
    1716             :                 case MYSQL_OPT_WRITE_TIMEOUT:
    1717             :                 case MYSQL_OPT_GUESS_CONNECTION:
    1718             :                 case MYSQL_OPT_USE_EMBEDDED_CONNECTION:
    1719             :                 case MYSQL_OPT_USE_REMOTE_CONNECTION:
    1720             :                 case MYSQL_SECURE_AUTH:
    1721             : #endif /* MySQL 4.1.1 */
    1722             : #ifdef MYSQL_OPT_RECONNECT
    1723             :                 case MYSQL_OPT_RECONNECT:
    1724             : #endif /* MySQL 5.0.13 */
    1725             : #ifdef MYSQL_OPT_SSL_VERIFY_SERVER_CERT
    1726             :                 case MYSQL_OPT_SSL_VERIFY_SERVER_CERT:
    1727             : #endif /* MySQL 5.0.23 */
    1728             : #ifdef MYSQL_OPT_COMPRESS
    1729             :                 case MYSQL_OPT_COMPRESS:
    1730             : #endif /* mysqlnd @ PHP 5.3.2 */
    1731             : #ifdef MYSQL_OPT_SSL_VERIFY_SERVER_CERT
    1732             :         REGISTER_LONG_CONSTANT("MYSQLI_OPT_SSL_VERIFY_SERVER_CERT", MYSQL_OPT_SSL_VERIFY_SERVER_CERT, CONST_CS | CONST_PERSISTENT);
    1733             : #endif /* MySQL 5.1.1., mysqlnd @ PHP 5.3.3 */
    1734             : #if (MYSQL_VERSION_ID >= 50611 && defined(CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS)) || defined(MYSQLI_USE_MYSQLND)
    1735             :                 case MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS:
    1736             : #endif
    1737          31 :                         return IS_LONG;
    1738             : 
    1739             : #ifdef MYSQL_SHARED_MEMORY_BASE_NAME
    1740             :                 case MYSQL_SHARED_MEMORY_BASE_NAME:
    1741             : #endif /* MySQL 4.1.0 */
    1742             : #ifdef MYSQL_SET_CLIENT_IP
    1743             :                 case MYSQL_SET_CLIENT_IP:
    1744             : #endif /* MySQL 4.1.1 */
    1745             :                 case MYSQL_READ_DEFAULT_FILE:
    1746             :                 case MYSQL_READ_DEFAULT_GROUP:
    1747             :                 case MYSQL_INIT_COMMAND:
    1748             :                 case MYSQL_SET_CHARSET_NAME:
    1749             :                 case MYSQL_SET_CHARSET_DIR:
    1750             : #if MYSQL_VERSION_ID > 50605 || defined(MYSQLI_USE_MYSQLND)
    1751             :                 case MYSQL_SERVER_PUBLIC_KEY:
    1752             : #endif
    1753          55 :                         return IS_STRING;
    1754             : 
    1755             :                 default:
    1756           2 :                         return IS_NULL;
    1757             :         }
    1758             : }
    1759             : /* }}} */
    1760             : 
    1761             : /* {{{ proto bool mysqli_options(object link, int flags, mixed values)
    1762             :    Set options */
    1763         101 : PHP_FUNCTION(mysqli_options)
    1764             : {
    1765             :         MY_MYSQL                *mysql;
    1766         101 :         zval                    *mysql_link = NULL;
    1767             :         zval                    *mysql_value;
    1768             :         zend_long                       mysql_option;
    1769             :         unsigned int    l_value;
    1770             :         zend_long                       ret;
    1771             :         int                             expected_type;
    1772             : 
    1773         101 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Olz", &mysql_link, mysqli_link_class_entry, &mysql_option, &mysql_value) == FAILURE) {
    1774          10 :                 return;
    1775             :         }
    1776          91 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_INITIALIZED);
    1777             : 
    1778             : #if PHP_API_VERSION < 20100412
    1779             :         if ((PG(open_basedir) && PG(open_basedir)[0] != '\0') || PG(safe_mode)) {
    1780             : #else
    1781          89 :         if (PG(open_basedir) && PG(open_basedir)[0] != '\0') {
    1782             : #endif
    1783           1 :                 if(mysql_option == MYSQL_OPT_LOCAL_INFILE) {
    1784           1 :                         RETURN_FALSE;
    1785             :                 }
    1786             :         }
    1787          88 :         expected_type = mysqli_options_get_option_zval_type(mysql_option);
    1788         176 :         if (expected_type != Z_TYPE_P(mysql_value)) {
    1789           4 :                 switch (expected_type) {
    1790             :                         case IS_STRING:
    1791           3 :                                 convert_to_string_ex(mysql_value);
    1792           1 :                                 break;
    1793             :                         case IS_LONG:
    1794           2 :                                 convert_to_long_ex(mysql_value);
    1795             :                                 break;
    1796             :                         default:
    1797             :                                 break;
    1798             :                 }
    1799             :         }
    1800          88 :         switch (expected_type) {
    1801             :                 case IS_STRING:
    1802          55 :                         ret = mysql_options(mysql->mysql, mysql_option, Z_STRVAL_P(mysql_value));
    1803          55 :                         break;
    1804             :                 case IS_LONG:
    1805          31 :                         l_value = Z_LVAL_P(mysql_value);
    1806          31 :                         ret = mysql_options(mysql->mysql, mysql_option, (char *)&l_value);
    1807          31 :                         break;
    1808             :                 default:
    1809           2 :                         ret = 1;
    1810             :                         break;
    1811             :         }
    1812             : 
    1813          88 :         RETURN_BOOL(!ret);
    1814             : }
    1815             : /* }}} */
    1816             : 
    1817             : /* {{{ proto bool mysqli_ping(object link)
    1818             :    Ping a server connection or reconnect if there is no connection */
    1819          14 : PHP_FUNCTION(mysqli_ping)
    1820             : {
    1821             :         MY_MYSQL        *mysql;
    1822             :         zval            *mysql_link;
    1823             :         zend_long               rc;
    1824             : 
    1825          14 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
    1826           2 :                 return;
    1827             :         }
    1828          12 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    1829          11 :         rc = mysql_ping(mysql->mysql);
    1830          11 :         MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
    1831             : 
    1832          11 :         RETURN_BOOL(!rc);
    1833             : }
    1834             : /* }}} */
    1835             : 
    1836             : /* {{{ proto mixed mysqli_prepare(object link, string query)
    1837             :    Prepare a SQL statement for execution */
    1838         128 : PHP_FUNCTION(mysqli_prepare)
    1839             : {
    1840             :         MY_MYSQL                *mysql;
    1841             :         MY_STMT                 *stmt;
    1842         128 :         char                    *query = NULL;
    1843             :         size_t                          query_len;
    1844             :         zval                    *mysql_link;
    1845             :         MYSQLI_RESOURCE *mysqli_resource;
    1846             : 
    1847         128 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os",&mysql_link, mysqli_link_class_entry, &query, &query_len) == FAILURE) {
    1848           2 :                 return;
    1849             :         }
    1850         126 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    1851             : 
    1852             : #if !defined(MYSQLI_USE_MYSQLND)
    1853             :         if (mysql->mysql->status == MYSQL_STATUS_GET_RESULT) {
    1854             :                 php_error_docref(NULL, E_WARNING, "All data must be fetched before a new statement prepare takes place");
    1855             :                 RETURN_FALSE;
    1856             :         }
    1857             : #endif
    1858             : 
    1859         126 :         stmt = (MY_STMT *)ecalloc(1,sizeof(MY_STMT));
    1860             : 
    1861         126 :         if ((stmt->stmt = mysql_stmt_init(mysql->mysql))) {
    1862         126 :                 if (mysql_stmt_prepare(stmt->stmt, query, query_len)) {
    1863             :                         /* mysql_stmt_close() clears errors, so we have to store them temporarily */
    1864             : #if !defined(MYSQLI_USE_MYSQLND)
    1865             :                         char  last_error[MYSQL_ERRMSG_SIZE];
    1866             :                         char  sqlstate[SQLSTATE_LENGTH+1];
    1867             :                         unsigned int last_errno;
    1868             : 
    1869             :                         last_errno = stmt->stmt->last_errno;
    1870             :                         memcpy(last_error, stmt->stmt->last_error, MYSQL_ERRMSG_SIZE);
    1871             :                         memcpy(sqlstate, mysql->mysql->net.sqlstate, SQLSTATE_LENGTH+1);
    1872             : #else
    1873          10 :                         MYSQLND_ERROR_INFO error_info = *mysql->mysql->data->error_info;
    1874             : #endif
    1875          10 :                         mysqli_stmt_close(stmt->stmt, FALSE);
    1876          10 :                         stmt->stmt = NULL;
    1877             : 
    1878             :                         /* restore error messages */
    1879             : #if !defined(MYSQLI_USE_MYSQLND)
    1880             :                         mysql->mysql->net.last_errno = last_errno;
    1881             :                         memcpy(mysql->mysql->net.last_error, last_error, MYSQL_ERRMSG_SIZE);
    1882             :                         memcpy(mysql->mysql->net.sqlstate, sqlstate, SQLSTATE_LENGTH+1);
    1883             : #else
    1884          10 :                         *mysql->mysql->data->error_info = error_info;
    1885             : #endif
    1886             :                 }
    1887             :         }
    1888             : 
    1889             :         /* don't initialize stmt->query with NULL, we ecalloc()-ed the memory */
    1890             :         /* Get performance boost if reporting is switched off */
    1891         126 :         if (stmt->stmt && query_len && (MyG(report_mode) & MYSQLI_REPORT_INDEX)) {
    1892           0 :                 stmt->query = (char *)emalloc(query_len + 1);
    1893           0 :                 memcpy(stmt->query, query, query_len);
    1894           0 :                 stmt->query[query_len] = '\0';
    1895             :         }
    1896             : 
    1897             :         /* don't join to the previous if because it won't work if mysql_stmt_prepare_fails */
    1898         126 :         if (!stmt->stmt) {
    1899          10 :                 MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
    1900          10 :                 efree(stmt);
    1901          10 :                 RETURN_FALSE;
    1902             :         }
    1903             : #ifndef MYSQLI_USE_MYSQLND
    1904             :         ZVAL_COPY(&stmt->link_handle, mysql_link);
    1905             : #endif
    1906             : 
    1907         116 :         mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
    1908         116 :         mysqli_resource->ptr = (void *)stmt;
    1909             : 
    1910             :         /* change status */
    1911         116 :         mysqli_resource->status = MYSQLI_STATUS_VALID;
    1912         116 :         MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_stmt_class_entry);
    1913             : }
    1914             : /* }}} */
    1915             : 
    1916             : /* {{{ proto bool mysqli_real_connect(object link [,string hostname [,string username [,string passwd [,string dbname [,int port [,string socket [,int flags]]]]]]])
    1917             :    Open a connection to a mysql server */
    1918        1180 : PHP_FUNCTION(mysqli_real_connect)
    1919             : {
    1920        1180 :         mysqli_common_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, TRUE, FALSE);
    1921        1180 : }
    1922             : /* }}} */
    1923             : 
    1924             : /* {{{ proto bool mysqli_real_query(object link, string query)
    1925             :    Binary-safe version of mysql_query() */
    1926          62 : PHP_FUNCTION(mysqli_real_query)
    1927             : {
    1928             :         MY_MYSQL        *mysql;
    1929             :         zval            *mysql_link;
    1930          62 :         char            *query = NULL;
    1931             :         size_t          query_len;
    1932             : 
    1933          62 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &mysql_link, mysqli_link_class_entry, &query, &query_len) == FAILURE) {
    1934           3 :                 return;
    1935             :         }
    1936          59 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    1937             : 
    1938          58 :         MYSQLI_DISABLE_MQ; /* disable multi statements/queries */
    1939             : 
    1940          58 :         if (mysql_real_query(mysql->mysql, query, query_len)) {
    1941           8 :                 MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
    1942           8 :                 RETURN_FALSE;
    1943             :         }
    1944             : 
    1945          50 :         if (!mysql_field_count(mysql->mysql)) {
    1946          16 :                 if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
    1947           0 :                         php_mysqli_report_index(query, mysqli_server_status(mysql->mysql));
    1948             :                 }
    1949             :         }
    1950             : 
    1951          50 :         RETURN_TRUE;
    1952             : }
    1953             : /* }}} */
    1954             : 
    1955             : /* {{{ proto string mysqli_real_escape_string(object link, string escapestr)
    1956             :    Escapes special characters in a string for use in a SQL statement, taking into account the current charset of the connection */
    1957       13534 : PHP_FUNCTION(mysqli_real_escape_string) {
    1958             :         MY_MYSQL        *mysql;
    1959       13534 :         zval            *mysql_link = NULL;
    1960             :         char            *escapestr;
    1961             :         size_t                  escapestr_len;
    1962             :         zend_string *newstr;
    1963             : 
    1964       13534 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &mysql_link, mysqli_link_class_entry, &escapestr, &escapestr_len) == FAILURE) {
    1965           7 :                 return;
    1966             :         }
    1967       13527 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    1968             : 
    1969       27050 :         newstr = zend_string_alloc(2 * escapestr_len, 0);
    1970       13525 :         newstr->len = mysql_real_escape_string(mysql->mysql, newstr->val, escapestr, escapestr_len);
    1971       27050 :         newstr = zend_string_truncate(newstr, newstr->len, 0);
    1972             : 
    1973       13525 :         RETURN_NEW_STR(newstr);
    1974             : }
    1975             : /* }}} */
    1976             : 
    1977             : /* {{{ proto bool mysqli_rollback(object link)
    1978             :    Undo actions from current transaction */
    1979          10 : PHP_FUNCTION(mysqli_rollback)
    1980             : {
    1981             :         MY_MYSQL        *mysql;
    1982             :         zval            *mysql_link;
    1983          10 :         zend_long               flags = TRANS_COR_NO_OPT;
    1984          10 :         char *          name = NULL;
    1985          10 :         size_t                  name_len = 0;
    1986             : 
    1987          10 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|ls", &mysql_link, mysqli_link_class_entry, &flags, &name, &name_len) == FAILURE) {
    1988           3 :                 return;
    1989             :         }
    1990           7 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    1991             : 
    1992             : #if !defined(MYSQLI_USE_MYSQLND)
    1993             :         if (mysqli_commit_or_rollback_libmysql(mysql->mysql, FALSE, flags, name)) {
    1994             : #else
    1995           6 :         if (FAIL == mysqlnd_rollback(mysql->mysql, flags, name)) {
    1996             : #endif
    1997           0 :                 RETURN_FALSE;
    1998             :         }
    1999           6 :         RETURN_TRUE;
    2000             : }
    2001             : /* }}} */
    2002             : 
    2003             : /* {{{ proto bool mysqli_stmt_send_long_data(object stmt, int param_nr, string data)
    2004             : */
    2005          23 : PHP_FUNCTION(mysqli_stmt_send_long_data)
    2006             : {
    2007             :         MY_STMT *stmt;
    2008             :         zval    *mysql_stmt;
    2009             :         char    *data;
    2010             :         zend_long       param_nr;
    2011             :         size_t          data_len;
    2012             : 
    2013          23 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ols", &mysql_stmt, mysqli_stmt_class_entry, &param_nr, &data, &data_len) == FAILURE) {
    2014           6 :                 return;
    2015             :         }
    2016          17 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
    2017             : 
    2018          17 :         if (param_nr < 0) {
    2019           1 :                 php_error_docref(NULL, E_WARNING, "Invalid parameter number");
    2020           1 :                 RETURN_FALSE;
    2021             :         }
    2022          16 :         if (mysql_stmt_send_long_data(stmt->stmt, param_nr, data, data_len)) {
    2023           1 :                 RETURN_FALSE;
    2024             :         }
    2025          15 :         RETURN_TRUE;
    2026             : }
    2027             : /* }}} */
    2028             : 
    2029             : /* {{{ proto mixed mysqli_stmt_affected_rows(object stmt)
    2030             :    Return the number of rows affected in the last query for the given link */
    2031          25 : PHP_FUNCTION(mysqli_stmt_affected_rows)
    2032             : {
    2033             :         MY_STMT                 *stmt;
    2034             :         zval                    *mysql_stmt;
    2035             :         my_ulonglong    rc;
    2036             : 
    2037          25 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
    2038           2 :                 return;
    2039             :         }
    2040          23 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
    2041             : 
    2042          21 :         rc = mysql_stmt_affected_rows(stmt->stmt);
    2043          21 :         if (rc == (my_ulonglong) -1) {
    2044           5 :                 RETURN_LONG(-1);
    2045             :         }
    2046          16 :         MYSQLI_RETURN_LONG_INT(rc)
    2047             : }
    2048             : /* }}} */
    2049             : 
    2050             : /* {{{ proto bool mysqli_stmt_close(object stmt)
    2051             :    Close statement */
    2052         918 : PHP_FUNCTION(mysqli_stmt_close)
    2053             : {
    2054             :         MY_STMT         *stmt;
    2055             :         zval            *mysql_stmt;
    2056             : 
    2057         918 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
    2058           2 :                 return;
    2059             :         }
    2060         916 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
    2061             : 
    2062         913 :         mysqli_stmt_close(stmt->stmt, FALSE);
    2063         913 :         stmt->stmt = NULL;
    2064         913 :         php_clear_stmt_bind(stmt);
    2065         913 :         MYSQLI_CLEAR_RESOURCE(mysql_stmt);
    2066         913 :         RETURN_TRUE;
    2067             : }
    2068             : /* }}} */
    2069             : 
    2070             : /* {{{ proto void mysqli_stmt_data_seek(object stmt, int offset)
    2071             :    Move internal result pointer */
    2072           8 : PHP_FUNCTION(mysqli_stmt_data_seek)
    2073             : {
    2074             :         MY_STMT         *stmt;
    2075             :         zval            *mysql_stmt;
    2076             :         zend_long               offset;
    2077             : 
    2078           8 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &mysql_stmt, mysqli_stmt_class_entry, &offset) == FAILURE) {
    2079           2 :                 return;
    2080             :         }
    2081           6 :         if (offset < 0) {
    2082           1 :                 php_error_docref(NULL, E_WARNING, "Offset must be positive");
    2083           1 :                 RETURN_FALSE;
    2084             :         }
    2085             : 
    2086           5 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
    2087             : 
    2088           3 :         mysql_stmt_data_seek(stmt->stmt, offset);
    2089             : }
    2090             : /* }}} */
    2091             : 
    2092             : /* {{{ proto int mysqli_stmt_field_count(object stmt) {
    2093             :    Return the number of result columns for the given statement */
    2094          20 : PHP_FUNCTION(mysqli_stmt_field_count)
    2095             : {
    2096             :         MY_STMT         *stmt;
    2097             :         zval            *mysql_stmt;
    2098             : 
    2099          20 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
    2100           2 :                 return;
    2101             :         }
    2102          18 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
    2103             : 
    2104          15 :         RETURN_LONG(mysql_stmt_field_count(stmt->stmt));
    2105             : }
    2106             : /* }}} */
    2107             : 
    2108             : /* {{{ proto void mysqli_stmt_free_result(object stmt)
    2109             :    Free stored result memory for the given statement handle */
    2110          53 : PHP_FUNCTION(mysqli_stmt_free_result)
    2111             : {
    2112             :         MY_STMT         *stmt;
    2113             :         zval            *mysql_stmt;
    2114             : 
    2115          53 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
    2116           2 :                 return;
    2117             :         }
    2118             : 
    2119          51 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
    2120             : 
    2121          49 :         mysql_stmt_free_result(stmt->stmt);
    2122             : }
    2123             : /* }}} */
    2124             : 
    2125             : /* {{{ proto mixed mysqli_stmt_insert_id(object stmt)
    2126             :    Get the ID generated from the previous INSERT operation */
    2127           8 : PHP_FUNCTION(mysqli_stmt_insert_id)
    2128             : {
    2129             :         MY_STMT                 *stmt;
    2130             :         my_ulonglong    rc;
    2131             :         zval                    *mysql_stmt;
    2132             : 
    2133           8 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
    2134           1 :                 return;
    2135             :         }
    2136           7 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
    2137           5 :         rc = mysql_stmt_insert_id(stmt->stmt);
    2138           5 :         MYSQLI_RETURN_LONG_INT(rc)
    2139             : }
    2140             : /* }}} */
    2141             : 
    2142             : /* {{{ proto int mysqli_stmt_param_count(object stmt)
    2143             :    Return the number of parameter for the given statement */
    2144          10 : PHP_FUNCTION(mysqli_stmt_param_count)
    2145             : {
    2146             :         MY_STMT         *stmt;
    2147             :         zval            *mysql_stmt;
    2148             : 
    2149          10 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
    2150           3 :                 return;
    2151             :         }
    2152           7 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
    2153             : 
    2154           5 :         RETURN_LONG(mysql_stmt_param_count(stmt->stmt));
    2155             : }
    2156             : /* }}} */
    2157             : 
    2158             : /* {{{ proto bool mysqli_stmt_reset(object stmt)
    2159             :    reset a prepared statement */
    2160          11 : PHP_FUNCTION(mysqli_stmt_reset)
    2161             : {
    2162             :         MY_STMT         *stmt;
    2163             :         zval            *mysql_stmt;
    2164             : 
    2165          11 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
    2166           3 :                 return;
    2167             :         }
    2168             : 
    2169           8 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
    2170             : 
    2171           6 :         if (mysql_stmt_reset(stmt->stmt)) {
    2172           0 :                 RETURN_FALSE;
    2173             :         }
    2174           6 :         RETURN_TRUE;
    2175             : }
    2176             : /* }}} */
    2177             : 
    2178             : /* {{{ proto mixed mysqli_stmt_num_rows(object stmt)
    2179             :    Return the number of rows in statements result set */
    2180          24 : PHP_FUNCTION(mysqli_stmt_num_rows)
    2181             : {
    2182             :         MY_STMT                 *stmt;
    2183             :         zval                    *mysql_stmt;
    2184             :         my_ulonglong    rc;
    2185             : 
    2186          24 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
    2187           2 :                 return;
    2188             :         }
    2189             : 
    2190          22 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
    2191             : 
    2192          21 :         rc = mysql_stmt_num_rows(stmt->stmt);
    2193          21 :         MYSQLI_RETURN_LONG_INT(rc)
    2194             : }
    2195             : /* }}} */
    2196             : 
    2197             : /* {{{ proto bool mysqli_select_db(object link, string dbname)
    2198             :    Select a MySQL database */
    2199          49 : PHP_FUNCTION(mysqli_select_db)
    2200             : {
    2201             :         MY_MYSQL        *mysql;
    2202             :         zval            *mysql_link;
    2203             :         char            *dbname;
    2204             :         size_t                  dbname_len;
    2205             : 
    2206          49 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &mysql_link, mysqli_link_class_entry, &dbname, &dbname_len) == FAILURE) {
    2207           3 :                 return;
    2208             :         }
    2209          46 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    2210             : 
    2211          45 :         if (mysql_select_db(mysql->mysql, dbname)) {
    2212           8 :                 MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
    2213           8 :                 RETURN_FALSE;
    2214             :         }
    2215          37 :         RETURN_TRUE;
    2216             : }
    2217             : /* }}} */
    2218             : 
    2219             : /* {{{ proto string mysqli_sqlstate(object link)
    2220             :    Returns the SQLSTATE error from previous MySQL operation */
    2221          16 : PHP_FUNCTION(mysqli_sqlstate)
    2222             : {
    2223             :         MY_MYSQL        *mysql;
    2224             :         zval            *mysql_link;
    2225             :         const char      *state;
    2226             : 
    2227          16 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
    2228           3 :                 return;
    2229             :         }
    2230          13 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    2231           6 :         state = mysql_sqlstate(mysql->mysql);
    2232           6 :         if (state) {
    2233          12 :                 RETURN_STRING(state);
    2234             :         }
    2235             : }
    2236             : /* }}} */
    2237             : 
    2238             : /* {{{ proto bool mysqli_ssl_set(object link ,string key ,string cert ,string ca ,string capath ,string cipher])
    2239             : */
    2240           9 : PHP_FUNCTION(mysqli_ssl_set)
    2241             : {
    2242             :         MY_MYSQL        *mysql;
    2243             :         zval            *mysql_link;
    2244             :         char            *ssl_parm[5];
    2245             :         size_t                  ssl_parm_len[5], i;
    2246             : 
    2247           9 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Osssss", &mysql_link, mysqli_link_class_entry, &ssl_parm[0], &ssl_parm_len[0], &ssl_parm[1], &ssl_parm_len[1], &ssl_parm[2], &ssl_parm_len[2], &ssl_parm[3], &ssl_parm_len[3], &ssl_parm[4], &ssl_parm_len[4])   == FAILURE) {
    2248           6 :                 return;
    2249             :         }
    2250           3 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_INITIALIZED);
    2251             : 
    2252          18 :         for (i = 0; i < 5; i++) {
    2253          15 :                 if (!ssl_parm_len[i]) {
    2254          10 :                         ssl_parm[i] = NULL;
    2255             :                 }
    2256             :         }
    2257             : 
    2258           3 :         mysql_ssl_set(mysql->mysql, ssl_parm[0], ssl_parm[1], ssl_parm[2], ssl_parm[3], ssl_parm[4]);
    2259             : 
    2260           3 :         RETURN_TRUE;
    2261             : }
    2262             : /* }}} */
    2263             : 
    2264             : /* {{{ proto mixed mysqli_stat(object link)
    2265             :    Get current system status */
    2266          11 : PHP_FUNCTION(mysqli_stat)
    2267             : {
    2268             :         MY_MYSQL        *mysql;
    2269             :         zval            *mysql_link;
    2270             : #if defined(MYSQLI_USE_MYSQLND)
    2271             :         zend_string *stat;
    2272             : #else
    2273             :         char            *stat;
    2274             : #endif
    2275             : 
    2276          11 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
    2277           3 :                 return;
    2278             :         }
    2279           8 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    2280             : 
    2281             : #if !defined(MYSQLI_USE_MYSQLND)
    2282             :         if ((stat = (char *)mysql_stat(mysql->mysql)))
    2283             :         {
    2284             :                 RETURN_STRING(stat);
    2285             : #else
    2286           7 :         if (mysqlnd_stat(mysql->mysql, &stat) == PASS)
    2287             :         {
    2288           7 :                 RETURN_STR(stat);
    2289             : #endif
    2290             :         } else {
    2291           0 :                 RETURN_FALSE;
    2292             :         }
    2293             : }
    2294             : 
    2295             : /* }}} */
    2296             : 
    2297             : /* {{{ proto bool mysqli_refresh(object link, long options)
    2298             :    Flush tables or caches, or reset replication server information */
    2299           0 : PHP_FUNCTION(mysqli_refresh)
    2300             : {
    2301             :         MY_MYSQL *mysql;
    2302           0 :         zval *mysql_link = NULL;
    2303             :         zend_long options;
    2304             : 
    2305           0 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &mysql_link, mysqli_link_class_entry, &options) == FAILURE) {
    2306           0 :                 return;
    2307             :         }
    2308           0 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_INITIALIZED);
    2309             : #ifdef MYSQLI_USE_MYSQLND
    2310           0 :         RETURN_BOOL(!mysql_refresh(mysql->mysql, (uint8_t) options));
    2311             : #else
    2312             :         RETURN_BOOL(!mysql_refresh(mysql->mysql, options));
    2313             : #endif
    2314             : }
    2315             : /* }}} */
    2316             : 
    2317             : /* {{{ proto int mysqli_stmt_attr_set(object stmt, long attr, long mode)
    2318             : */
    2319        1116 : PHP_FUNCTION(mysqli_stmt_attr_set)
    2320             : {
    2321             :         MY_STMT *stmt;
    2322             :         zval    *mysql_stmt;
    2323             :         zend_long       mode_in;
    2324             : #if MYSQL_VERSION_ID >= 50107
    2325             :         my_bool mode_b;
    2326             : #endif
    2327             :         zend_ulong      mode;
    2328             :         zend_long       attr;
    2329             :         void    *mode_p;
    2330             : 
    2331        1116 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oll", &mysql_stmt, mysqli_stmt_class_entry, &attr, &mode_in) == FAILURE) {
    2332           4 :                 return;
    2333             :         }
    2334        1112 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
    2335             : 
    2336        1111 :         if (mode_in < 0) {
    2337           0 :                 php_error_docref(NULL, E_WARNING, "mode should be non-negative, %pd passed", mode_in);
    2338           0 :                 RETURN_FALSE;
    2339             :         }
    2340             : 
    2341        1111 :         switch (attr) {
    2342             : #if MYSQL_VERSION_ID >= 50107
    2343             :         case STMT_ATTR_UPDATE_MAX_LENGTH:
    2344             :                 mode_b = (my_bool) mode_in;
    2345             :                 mode_p = &mode_b;
    2346             :                 break;
    2347             : #endif
    2348             :         default:
    2349        1111 :                 mode = mode_in;
    2350        1111 :                 mode_p = &mode;
    2351             :                 break;
    2352             :         }
    2353             : #if !defined(MYSQLI_USE_MYSQLND)
    2354             :         if (mysql_stmt_attr_set(stmt->stmt, attr, mode_p)) {
    2355             : #else
    2356        1111 :         if (FAIL == mysql_stmt_attr_set(stmt->stmt, attr, mode_p)) {
    2357             : #endif
    2358        1101 :                 RETURN_FALSE;
    2359             :         }
    2360          10 :         RETURN_TRUE;
    2361             : }
    2362             : /* }}} */
    2363             : 
    2364             : /* {{{ proto int mysqli_stmt_attr_get(object stmt, long attr)
    2365             : */
    2366          13 : PHP_FUNCTION(mysqli_stmt_attr_get)
    2367             : {
    2368             :         MY_STMT *stmt;
    2369             :         zval    *mysql_stmt;
    2370          13 :         zend_ulong      value = 0;
    2371             :         zend_long       attr;
    2372             :         int             rc;
    2373             : 
    2374          13 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &mysql_stmt, mysqli_stmt_class_entry, &attr) == FAILURE) {
    2375           4 :                 return;
    2376             :         }
    2377           9 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
    2378             : 
    2379           6 :         if ((rc = mysql_stmt_attr_get(stmt->stmt, attr, &value))) {
    2380           1 :                 RETURN_FALSE;
    2381             :         }
    2382             : 
    2383             : #if MYSQL_VERSION_ID >= 50107
    2384             :         if (attr == STMT_ATTR_UPDATE_MAX_LENGTH)
    2385             :                 value = *((my_bool *)&value);
    2386             : #endif
    2387           5 :         RETURN_LONG((zend_ulong)value);
    2388             : }
    2389             : /* }}} */
    2390             : 
    2391             : /* {{{ proto int mysqli_stmt_errno(object stmt)
    2392             : */
    2393          11 : PHP_FUNCTION(mysqli_stmt_errno)
    2394             : {
    2395             :         MY_STMT *stmt;
    2396             :         zval    *mysql_stmt;
    2397             : 
    2398          11 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
    2399           2 :                 return;
    2400             :         }
    2401           9 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_INITIALIZED);
    2402             : 
    2403           8 :         RETURN_LONG(mysql_stmt_errno(stmt->stmt));
    2404             : }
    2405             : /* }}} */
    2406             : 
    2407             : /* {{{ proto string mysqli_stmt_error(object stmt)
    2408             : */
    2409          10 : PHP_FUNCTION(mysqli_stmt_error)
    2410             : {
    2411             :         MY_STMT *stmt;
    2412             :         zval    *mysql_stmt;
    2413             :         const char * err;
    2414             : 
    2415          10 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
    2416           2 :                 return;
    2417             :         }
    2418           8 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_INITIALIZED);
    2419             : 
    2420           7 :         err = mysql_stmt_error(stmt->stmt);
    2421           7 :         if (err) {
    2422          14 :                 RETURN_STRING(err);
    2423             :         }
    2424             : }
    2425             : /* }}} */
    2426             : 
    2427             : /* {{{ proto mixed mysqli_stmt_init(object link)
    2428             :    Initialize statement object
    2429             : */
    2430         830 : PHP_FUNCTION(mysqli_stmt_init)
    2431             : {
    2432             :         MY_MYSQL                *mysql;
    2433             :         MY_STMT                 *stmt;
    2434             :         zval                    *mysql_link;
    2435             :         MYSQLI_RESOURCE *mysqli_resource;
    2436             : 
    2437         830 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",&mysql_link, mysqli_link_class_entry) == FAILURE) {
    2438           3 :                 return;
    2439             :         }
    2440         827 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    2441             : 
    2442         826 :         stmt = (MY_STMT *)ecalloc(1,sizeof(MY_STMT));
    2443             : 
    2444         826 :         if (!(stmt->stmt = mysql_stmt_init(mysql->mysql))) {
    2445           0 :                 efree(stmt);
    2446           0 :                 RETURN_FALSE;
    2447             :         }
    2448             : #ifndef MYSQLI_USE_MYSQLND
    2449             :         ZVAL_COPY(&stmt->link_handle, mysql_link);
    2450             : #endif
    2451             : 
    2452         826 :         mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
    2453         826 :         mysqli_resource->status = MYSQLI_STATUS_INITIALIZED;
    2454         826 :         mysqli_resource->ptr = (void *)stmt;
    2455         826 :         MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_stmt_class_entry);
    2456             : }
    2457             : /* }}} */
    2458             : 
    2459             : /* {{{ proto bool mysqli_stmt_prepare(object stmt, string query)
    2460             :    prepare server side statement with query
    2461             : */
    2462        3259 : PHP_FUNCTION(mysqli_stmt_prepare)
    2463             : {
    2464             :         MY_STMT *stmt;
    2465             :         zval    *mysql_stmt;
    2466             :         char    *query;
    2467             :         size_t          query_len;
    2468             : 
    2469        3259 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &mysql_stmt, mysqli_stmt_class_entry, &query, &query_len) == FAILURE) {
    2470           5 :                 return;
    2471             :         }
    2472        3254 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_INITIALIZED);
    2473             : 
    2474        3252 :         if (mysql_stmt_prepare(stmt->stmt, query, query_len)) {
    2475          10 :                 MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
    2476          10 :                 RETURN_FALSE;
    2477             :         }
    2478             :         /* change status */
    2479        3242 :         MYSQLI_SET_STATUS(mysql_stmt, MYSQLI_STATUS_VALID);
    2480        3242 :         RETURN_TRUE;
    2481             : }
    2482             : /* }}} */
    2483             : 
    2484             : /* {{{ proto mixed mysqli_stmt_result_metadata(object stmt)
    2485             :    return result set from statement */
    2486         221 : PHP_FUNCTION(mysqli_stmt_result_metadata)
    2487             : {
    2488             :         MY_STMT                 *stmt;
    2489             :         MYSQL_RES               *result;
    2490             :         zval                    *mysql_stmt;
    2491             :         MYSQLI_RESOURCE *mysqli_resource;
    2492             : 
    2493         221 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
    2494           3 :                 return;
    2495             :         }
    2496         218 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
    2497             : 
    2498         216 :         if (!(result = mysql_stmt_result_metadata(stmt->stmt))){
    2499           0 :                 MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
    2500           0 :                 RETURN_FALSE;
    2501             :         }
    2502             : 
    2503         216 :         mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
    2504         216 :         mysqli_resource->ptr = (void *)result;
    2505         216 :         mysqli_resource->status = MYSQLI_STATUS_VALID;
    2506         216 :         MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_result_class_entry);
    2507             : }
    2508             : /* }}} */
    2509             : 
    2510             : /* {{{ proto bool mysqli_stmt_store_result(object stmt)
    2511             : */
    2512          60 : PHP_FUNCTION(mysqli_stmt_store_result)
    2513             : {
    2514             :         MY_STMT *stmt;
    2515             :         zval    *mysql_stmt;
    2516             : 
    2517          60 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
    2518           2 :                 return;
    2519             :         }
    2520          58 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
    2521             : 
    2522             : #if !defined(MYSQLI_USE_MYSQLND)
    2523             :         {
    2524             :                 /*
    2525             :                   If the user wants to store the data and we have BLOBs/TEXTs we try to allocate
    2526             :                   not the maximal length of the type (which is 16MB even for LONGBLOB) but
    2527             :                   the maximal length of the field in the result set. If he/she has quite big
    2528             :                   BLOB/TEXT columns after calling store_result() the memory usage of PHP will
    2529             :                   double - but this is a known problem of the simple MySQL API ;)
    2530             :                 */
    2531             :                 int     i = 0;
    2532             : 
    2533             :                 for (i = mysql_stmt_field_count(stmt->stmt) - 1; i >=0; --i) {
    2534             :                         if (stmt->stmt->fields && (stmt->stmt->fields[i].type == MYSQL_TYPE_BLOB ||
    2535             :                                 stmt->stmt->fields[i].type == MYSQL_TYPE_MEDIUM_BLOB ||
    2536             :                                 stmt->stmt->fields[i].type == MYSQL_TYPE_LONG_BLOB ||
    2537             :                                 stmt->stmt->fields[i].type == MYSQL_TYPE_GEOMETRY))
    2538             :                         {
    2539             : #if MYSQL_VERSION_ID >= 50107
    2540             :                                 my_bool tmp=1;
    2541             : #else
    2542             :                                 uint tmp=1;
    2543             : #endif
    2544             :                                 mysql_stmt_attr_set(stmt->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &tmp);
    2545             :                                 break;
    2546             :                         }
    2547             :                 }
    2548             :         }
    2549             : #endif
    2550             : 
    2551          55 :         if (mysql_stmt_store_result(stmt->stmt)){
    2552           3 :                 MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
    2553           3 :                 RETURN_FALSE;
    2554             :         }
    2555          52 :         RETURN_TRUE;
    2556             : }
    2557             : /* }}} */
    2558             : 
    2559             : /* {{{ proto string mysqli_stmt_sqlstate(object stmt)
    2560             : */
    2561           8 : PHP_FUNCTION(mysqli_stmt_sqlstate)
    2562             : {
    2563             :         MY_STMT *stmt;
    2564             :         zval    *mysql_stmt;
    2565             :         const char * state;
    2566             : 
    2567           8 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
    2568           3 :                 return;
    2569             :         }
    2570           5 :         MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
    2571             : 
    2572           3 :         state = mysql_stmt_sqlstate(stmt->stmt);
    2573           3 :         if (state) {
    2574           6 :                 RETURN_STRING(state);
    2575             :         }
    2576             : }
    2577             : /* }}} */
    2578             : 
    2579             : /* {{{ proto object mysqli_store_result(object link [, int flags])
    2580             :    Buffer result set on client */
    2581          66 : PHP_FUNCTION(mysqli_store_result)
    2582             : {
    2583             :         MY_MYSQL                *mysql;
    2584             :         MYSQL_RES               *result;
    2585             :         zval                    *mysql_link;
    2586             :         MYSQLI_RESOURCE *mysqli_resource;
    2587          66 :         zend_long flags = 0;
    2588             : 
    2589             : 
    2590          66 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|l", &mysql_link, mysqli_link_class_entry, &flags) == FAILURE) {
    2591           2 :                 return;
    2592             :         }
    2593          64 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    2594             : #if MYSQLI_USE_MYSQLND
    2595          63 :         result = flags & MYSQLI_STORE_RESULT_COPY_DATA? mysqlnd_store_result_ofs(mysql->mysql) : mysqlnd_store_result(mysql->mysql);
    2596             : #else
    2597             :         result = mysql_store_result(mysql->mysql);
    2598             : #endif
    2599          63 :         if (!result) {
    2600          15 :                 MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
    2601          15 :                 RETURN_FALSE;
    2602             :         }
    2603          48 :         if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
    2604           0 :                 php_mysqli_report_index("from previous query", mysqli_server_status(mysql->mysql));
    2605             :         }
    2606             : 
    2607          48 :         mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
    2608          48 :         mysqli_resource->ptr = (void *)result;
    2609          48 :         mysqli_resource->status = MYSQLI_STATUS_VALID;
    2610          48 :         MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_result_class_entry);
    2611             : }
    2612             : /* }}} */
    2613             : 
    2614             : /* {{{ proto int mysqli_thread_id(object link)
    2615             :    Return the current thread ID */
    2616          79 : PHP_FUNCTION(mysqli_thread_id)
    2617             : {
    2618             :         MY_MYSQL        *mysql;
    2619             :         zval            *mysql_link;
    2620             : 
    2621          79 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
    2622           2 :                 return;
    2623             :         }
    2624          77 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    2625             : 
    2626          68 :         RETURN_LONG((zend_long) mysql_thread_id(mysql->mysql));
    2627             : }
    2628             : /* }}} */
    2629             : 
    2630             : /* {{{ proto bool mysqli_thread_safe(void)
    2631             :    Return whether thread safety is given or not */
    2632           2 : PHP_FUNCTION(mysqli_thread_safe)
    2633             : {
    2634           2 :         RETURN_BOOL(mysql_thread_safe());
    2635             : }
    2636             : /* }}} */
    2637             : 
    2638             : /* {{{ proto mixed mysqli_use_result(object link)
    2639             :    Directly retrieve query results - do not buffer results on client side */
    2640          23 : PHP_FUNCTION(mysqli_use_result)
    2641             : {
    2642             :         MY_MYSQL                *mysql;
    2643             :         MYSQL_RES               *result;
    2644             :         zval                    *mysql_link;
    2645             :         MYSQLI_RESOURCE *mysqli_resource;
    2646             : 
    2647          23 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
    2648           2 :                 return;
    2649             :         }
    2650          21 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    2651             : 
    2652          20 :         if (!(result = mysql_use_result(mysql->mysql))) {
    2653           3 :                 MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
    2654           3 :                 RETURN_FALSE;
    2655             :         }
    2656             : 
    2657          17 :         if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
    2658           0 :                 php_mysqli_report_index("from previous query", mysqli_server_status(mysql->mysql));
    2659             :         }
    2660          17 :         mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
    2661          17 :         mysqli_resource->ptr = (void *)result;
    2662          17 :         mysqli_resource->status = MYSQLI_STATUS_VALID;
    2663          17 :         MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_result_class_entry);
    2664             : }
    2665             : /* }}} */
    2666             : 
    2667             : /* {{{ proto int mysqli_warning_count (object link)
    2668             :    Return number of warnings from the last query for the given link */
    2669          16 : PHP_FUNCTION(mysqli_warning_count)
    2670             : {
    2671             :         MY_MYSQL        *mysql;
    2672             :         zval            *mysql_link;
    2673             : 
    2674          16 :         if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
    2675           3 :                 return;
    2676             :         }
    2677          13 :         MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
    2678             : 
    2679           6 :         RETURN_LONG(mysql_warning_count(mysql->mysql));
    2680             : }
    2681             : /* }}} */
    2682             : 
    2683             : /*
    2684             :  * Local variables:
    2685             :  * tab-width: 4
    2686             :  * c-basic-offset: 4
    2687             :  * End:
    2688             :  * vim600: noet sw=4 ts=4 fdm=marker
    2689             :  * vim<600: noet sw=4 ts=4
    2690             :  */

Generated by: LCOV version 1.10

Generated at Sat, 27 Jun 2015 09:41:12 +0000 (3 days ago)

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