mysqli_api.c 77 KB

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