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

Generated by: LCOV version 1.10

Generated at Mon, 26 Jan 2015 14:46:47 +0000 (17 hours ago)

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