mysqli_api.c 75 KB

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