mysqli_api.c 73 KB

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