PageRenderTime 63ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/php5/ext/mysqli/mysqli_api.c

http://github.com/vpj/PHP-Extension-API
C | 2421 lines | 1709 code | 352 blank | 360 comment | 359 complexity | f2fd859e37d1ce473d21251f50f58529 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2009 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: mysqli_api.c,v 1.118.2.22.2.16.2.24 2009/01/22 21:01:56 johannes Exp $
  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 "php_mysqli_structs.h"
  30. /* {{{ proto mixed mysqli_affected_rows(object link)
  31. Get number of affected rows in previous MySQL operation */
  32. PHP_FUNCTION(mysqli_affected_rows)
  33. {
  34. MY_MYSQL *mysql;
  35. zval *mysql_link;
  36. my_ulonglong rc;
  37. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
  38. return;
  39. }
  40. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
  41. rc = mysql_affected_rows(mysql->mysql);
  42. if (rc == (my_ulonglong) -1) {
  43. RETURN_LONG(-1);
  44. }
  45. MYSQLI_RETURN_LONG_LONG(rc);
  46. }
  47. /* }}} */
  48. /* {{{ proto bool mysqli_autocommit(object link, bool mode)
  49. Turn auto commit on or of */
  50. PHP_FUNCTION(mysqli_autocommit)
  51. {
  52. MY_MYSQL *mysql;
  53. zval *mysql_link;
  54. zend_bool automode;
  55. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ob", &mysql_link, mysqli_link_class_entry, &automode) == FAILURE) {
  56. return;
  57. }
  58. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
  59. if (mysql_autocommit(mysql->mysql, (my_bool)automode)) {
  60. RETURN_FALSE;
  61. }
  62. RETURN_TRUE;
  63. }
  64. /* }}} */
  65. /* {{{ mysqli_stmt_bind_param_do_bind */
  66. #ifndef MYSQLI_USE_MYSQLND
  67. static
  68. int mysqli_stmt_bind_param_do_bind(MY_STMT *stmt, unsigned int argc, unsigned int num_vars,
  69. zval ***args, unsigned int start, const char * const types TSRMLS_DC)
  70. {
  71. int i, ofs;
  72. MYSQL_BIND *bind;
  73. unsigned long rc;
  74. /* prevent leak if variables are already bound */
  75. if (stmt->param.var_cnt) {
  76. php_free_stmt_bind_buffer(stmt->param, FETCH_SIMPLE);
  77. }
  78. stmt->param.is_null = ecalloc(num_vars, sizeof(char));
  79. bind = (MYSQL_BIND *) ecalloc(num_vars, sizeof(MYSQL_BIND));
  80. ofs = 0;
  81. for (i = start; i < argc; i++) {
  82. /* set specified type */
  83. switch (types[ofs]) {
  84. case 'd': /* Double */
  85. bind[ofs].buffer_type = MYSQL_TYPE_DOUBLE;
  86. bind[ofs].buffer = &Z_DVAL_PP(args[i]);
  87. bind[ofs].is_null = &stmt->param.is_null[ofs];
  88. break;
  89. case 'i': /* Integer */
  90. #if SIZEOF_LONG==8
  91. bind[ofs].buffer_type = MYSQL_TYPE_LONGLONG;
  92. #elif SIZEOF_LONG==4
  93. bind[ofs].buffer_type = MYSQL_TYPE_LONG;
  94. #endif
  95. bind[ofs].buffer = &Z_LVAL_PP(args[i]);
  96. bind[ofs].is_null = &stmt->param.is_null[ofs];
  97. break;
  98. case 'b': /* Blob (send data) */
  99. bind[ofs].buffer_type = MYSQL_TYPE_LONG_BLOB;
  100. /* don't initialize is_null and length to 0 because we use ecalloc */
  101. break;
  102. case 's': /* string */
  103. bind[ofs].buffer_type = MYSQL_TYPE_VAR_STRING;
  104. /* don't initialize buffer and buffer_length because we use ecalloc */
  105. bind[ofs].is_null = &stmt->param.is_null[ofs];
  106. break;
  107. default:
  108. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Undefined fieldtype %c (parameter %d)", types[ofs], i+1);
  109. rc = 1;
  110. goto end_1;
  111. }
  112. ofs++;
  113. }
  114. rc = mysql_stmt_bind_param(stmt->stmt, bind);
  115. end_1:
  116. if (rc) {
  117. efree(stmt->param.is_null);
  118. } else {
  119. stmt->param.var_cnt = num_vars;
  120. stmt->param.vars = (zval **)safe_emalloc(num_vars, sizeof(zval), 0);
  121. for (i = 0; i < num_vars; i++) {
  122. if (bind[i].buffer_type != MYSQL_TYPE_LONG_BLOB) {
  123. Z_ADDREF_P(*args[i+start]);
  124. stmt->param.vars[i] = *args[i+start];
  125. } else {
  126. stmt->param.vars[i] = NULL;
  127. }
  128. }
  129. }
  130. efree(bind);
  131. return rc;
  132. }
  133. #else
  134. static
  135. int mysqli_stmt_bind_param_do_bind(MY_STMT *stmt, unsigned int argc, unsigned int num_vars,
  136. zval ***args, unsigned int start, const char * const types TSRMLS_DC)
  137. {
  138. int i;
  139. MYSQLND_PARAM_BIND *params;
  140. enum_func_status ret = FAIL;
  141. /* If no params -> skip binding and return directly */
  142. if (argc == start) {
  143. return PASS;
  144. }
  145. params = safe_emalloc(argc - start, sizeof(MYSQLND_PARAM_BIND), 0);
  146. for (i = 0; i < (argc - start); i++) {
  147. zend_uchar type;
  148. switch (types[i]) {
  149. case 'd': /* Double */
  150. type = MYSQL_TYPE_DOUBLE;
  151. break;
  152. case 'i': /* Integer */
  153. #if SIZEOF_LONG==8
  154. type = MYSQL_TYPE_LONGLONG;
  155. #elif SIZEOF_LONG==4
  156. type = MYSQL_TYPE_LONG;
  157. #endif
  158. break;
  159. case 'b': /* Blob (send data) */
  160. type = MYSQL_TYPE_LONG_BLOB;
  161. break;
  162. case 's': /* string */
  163. type = MYSQL_TYPE_VAR_STRING;
  164. break;
  165. default:
  166. /* We count parameters from 1 */
  167. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Undefined fieldtype %c (parameter %d)", types[i], i + start + 1);
  168. ret = FAIL;
  169. efree(params);
  170. goto end;
  171. }
  172. params[i].zv = *(args[i + start]);
  173. params[i].type = type;
  174. }
  175. ret = mysqlnd_stmt_bind_param(stmt->stmt, params);
  176. end:
  177. return ret;
  178. }
  179. #endif
  180. /* }}} */
  181. /* {{{ proto bool mysqli_stmt_bind_param(object stmt, string types, mixed variable [,mixed,....]) U
  182. Bind variables to a prepared statement as parameters */
  183. PHP_FUNCTION(mysqli_stmt_bind_param)
  184. {
  185. zval ***args;
  186. int argc = ZEND_NUM_ARGS();
  187. int num_vars;
  188. int start = 2;
  189. MY_STMT *stmt;
  190. zval *mysql_stmt;
  191. char *types;
  192. int types_len;
  193. unsigned long rc;
  194. /* calculate and check number of parameters */
  195. if (argc < 2) {
  196. /* there has to be at least one pair */
  197. WRONG_PARAM_COUNT;
  198. }
  199. if (zend_parse_method_parameters((getThis()) ? 1:2 TSRMLS_CC, getThis(), "Os", &mysql_stmt, mysqli_stmt_class_entry,
  200. &types, &types_len) == FAILURE) {
  201. return;
  202. }
  203. MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
  204. num_vars = argc - 1;
  205. if (getThis()) {
  206. start = 1;
  207. } else {
  208. /* ignore handle parameter in procedural interface*/
  209. --num_vars;
  210. }
  211. if (!types_len) {
  212. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid type or no types specified");
  213. RETURN_FALSE;
  214. }
  215. if (types_len != argc - start) {
  216. /* number of bind variables doesn't match number of elements in type definition string */
  217. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number of elements in type definition string doesn't match number of bind variables");
  218. RETURN_FALSE;
  219. }
  220. if (types_len != mysql_stmt_param_count(stmt->stmt)) {
  221. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number of variables doesn't match number of parameters in prepared statement");
  222. RETURN_FALSE;
  223. }
  224. args = (zval ***)safe_emalloc(argc, sizeof(zval **), 0);
  225. if (zend_get_parameters_array_ex(argc, args) == FAILURE) {
  226. zend_wrong_param_count(TSRMLS_C);
  227. rc = 1;
  228. } else {
  229. rc = mysqli_stmt_bind_param_do_bind(stmt, argc, num_vars, args, start, types TSRMLS_CC);
  230. MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
  231. }
  232. efree(args);
  233. RETURN_BOOL(!rc);
  234. }
  235. /* }}} */
  236. /* {{{ mysqli_stmt_bind_result_do_bind */
  237. #ifndef MYSQLI_USE_MYSQLND
  238. /* TODO:
  239. do_alloca, free_alloca
  240. */
  241. static int
  242. mysqli_stmt_bind_result_do_bind(MY_STMT *stmt, zval ***args, unsigned int argc, unsigned int start TSRMLS_DC)
  243. {
  244. MYSQL_BIND *bind;
  245. int i, ofs;
  246. int var_cnt = argc - start;
  247. long col_type;
  248. ulong rc;
  249. /* prevent leak if variables are already bound */
  250. if (stmt->result.var_cnt) {
  251. php_free_stmt_bind_buffer(stmt->result, FETCH_RESULT);
  252. }
  253. bind = (MYSQL_BIND *)ecalloc(var_cnt, sizeof(MYSQL_BIND));
  254. {
  255. int size;
  256. char *p= emalloc(size= var_cnt * (sizeof(char) + sizeof(VAR_BUFFER)));
  257. stmt->result.buf = (VAR_BUFFER *) p;
  258. stmt->result.is_null = p + var_cnt * sizeof(VAR_BUFFER);
  259. memset(p, 0, size);
  260. }
  261. for (i=start; i < var_cnt + start ; i++) {
  262. ofs = i - start;
  263. col_type = (stmt->stmt->fields) ? stmt->stmt->fields[ofs].type : MYSQL_TYPE_STRING;
  264. switch (col_type) {
  265. case MYSQL_TYPE_DOUBLE:
  266. case MYSQL_TYPE_FLOAT:
  267. convert_to_double_ex(args[i]);
  268. stmt->result.buf[ofs].type = IS_DOUBLE;
  269. stmt->result.buf[ofs].buflen = sizeof(double);
  270. /* allocate buffer for double */
  271. stmt->result.buf[ofs].val = (char *)emalloc(sizeof(double));
  272. bind[ofs].buffer_type = MYSQL_TYPE_DOUBLE;
  273. bind[ofs].buffer = stmt->result.buf[ofs].val;
  274. bind[ofs].is_null = &stmt->result.is_null[ofs];
  275. break;
  276. case MYSQL_TYPE_NULL:
  277. stmt->result.buf[ofs].type = IS_NULL;
  278. /*
  279. don't initialize to 0 :
  280. 1. stmt->result.buf[ofs].buflen
  281. 2. bind[ofs].buffer
  282. 3. bind[ofs].buffer_length
  283. because memory was allocated with ecalloc
  284. */
  285. bind[ofs].buffer_type = MYSQL_TYPE_NULL;
  286. bind[ofs].is_null = &stmt->result.is_null[ofs];
  287. break;
  288. case MYSQL_TYPE_SHORT:
  289. case MYSQL_TYPE_TINY:
  290. case MYSQL_TYPE_LONG:
  291. case MYSQL_TYPE_INT24:
  292. case MYSQL_TYPE_YEAR:
  293. convert_to_long_ex(args[i]);
  294. stmt->result.buf[ofs].type = IS_LONG;
  295. /* don't set stmt->result.buf[ofs].buflen to 0, we used ecalloc */
  296. stmt->result.buf[ofs].val = (char *)emalloc(sizeof(int));
  297. bind[ofs].buffer_type = MYSQL_TYPE_LONG;
  298. bind[ofs].buffer = stmt->result.buf[ofs].val;
  299. bind[ofs].is_null = &stmt->result.is_null[ofs];
  300. bind[ofs].is_unsigned = (stmt->stmt->fields[ofs].flags & UNSIGNED_FLAG) ? 1 : 0;
  301. break;
  302. case MYSQL_TYPE_LONGLONG:
  303. #if MYSQL_VERSION_ID > 50002 || defined(MYSQLI_USE_MYSQLND)
  304. case MYSQL_TYPE_BIT:
  305. #endif
  306. stmt->result.buf[ofs].type = IS_STRING;
  307. stmt->result.buf[ofs].buflen = sizeof(my_ulonglong);
  308. stmt->result.buf[ofs].val = (char *)emalloc(stmt->result.buf[ofs].buflen);
  309. bind[ofs].buffer_type = col_type;
  310. bind[ofs].buffer = stmt->result.buf[ofs].val;
  311. bind[ofs].is_null = &stmt->result.is_null[ofs];
  312. bind[ofs].buffer_length = stmt->result.buf[ofs].buflen;
  313. bind[ofs].is_unsigned = (stmt->stmt->fields[ofs].flags & UNSIGNED_FLAG) ? 1 : 0;
  314. break;
  315. case MYSQL_TYPE_DATE:
  316. case MYSQL_TYPE_TIME:
  317. case MYSQL_TYPE_DATETIME:
  318. case MYSQL_TYPE_NEWDATE:
  319. case MYSQL_TYPE_VAR_STRING:
  320. case MYSQL_TYPE_STRING:
  321. case MYSQL_TYPE_TINY_BLOB:
  322. case MYSQL_TYPE_BLOB:
  323. case MYSQL_TYPE_MEDIUM_BLOB:
  324. case MYSQL_TYPE_LONG_BLOB:
  325. case MYSQL_TYPE_TIMESTAMP:
  326. case MYSQL_TYPE_DECIMAL:
  327. #ifdef FIELD_TYPE_NEWDECIMAL
  328. case MYSQL_TYPE_NEWDECIMAL:
  329. #endif
  330. {
  331. #if MYSQL_VERSION_ID > 50099
  332. /* Changed to my_bool in MySQL 5.1. See MySQL Bug #16144 */
  333. my_bool tmp;
  334. #else
  335. ulong tmp = 0;
  336. #endif
  337. stmt->result.buf[ofs].type = IS_STRING;
  338. /*
  339. If the user has called $stmt->store_result() then we have asked
  340. max_length to be updated. this is done only for BLOBS because we don't want to allocate
  341. big chunkgs of memory 2^16 or 2^24
  342. */
  343. if (stmt->stmt->fields[ofs].max_length == 0 &&
  344. !mysql_stmt_attr_get(stmt->stmt, STMT_ATTR_UPDATE_MAX_LENGTH, &tmp) && !tmp)
  345. {
  346. /*
  347. Allocate directly 256 because it's easier to allocate a bit more
  348. than update max length even for text columns. Try SELECT UNION SELECT UNION with
  349. different lengths and you will see that we get different lengths in stmt->stmt->fields[ofs].length
  350. The just take 256 and saves us from realloc-ing.
  351. */
  352. stmt->result.buf[ofs].buflen = 256;
  353. } else {
  354. /*
  355. the user has called store_result(). if he does not there is no way to determine the
  356. libmysql does not allow us to allocate 0 bytes for a buffer so we try 1
  357. */
  358. if (!(stmt->result.buf[ofs].buflen = stmt->stmt->fields[ofs].max_length))
  359. ++stmt->result.buf[ofs].buflen;
  360. }
  361. stmt->result.buf[ofs].val = (char *)emalloc(stmt->result.buf[ofs].buflen);
  362. bind[ofs].buffer_type = MYSQL_TYPE_STRING;
  363. bind[ofs].buffer = stmt->result.buf[ofs].val;
  364. bind[ofs].is_null = &stmt->result.is_null[ofs];
  365. bind[ofs].buffer_length = stmt->result.buf[ofs].buflen;
  366. bind[ofs].length = &stmt->result.buf[ofs].buflen;
  367. break;
  368. }
  369. default:
  370. 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);
  371. break;
  372. }
  373. }
  374. rc = mysql_stmt_bind_result(stmt->stmt, bind);
  375. MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
  376. if (rc) {
  377. /* dont close the statement or subsequent usage (for example ->execute()) will lead to crash */
  378. for (i=0; i < var_cnt ; i++) {
  379. if (stmt->result.buf[i].val) {
  380. efree(stmt->result.buf[i].val);
  381. }
  382. }
  383. /* Don't free stmt->result.is_null because is_null & buf are one block of memory */
  384. efree(stmt->result.buf);
  385. } else {
  386. stmt->result.var_cnt = var_cnt;
  387. stmt->result.vars = (zval **)safe_emalloc((var_cnt), sizeof(zval), 0);
  388. for (i = start; i < var_cnt+start; i++) {
  389. ofs = i-start;
  390. Z_ADDREF_PP(args[i]);
  391. stmt->result.vars[ofs] = *args[i];
  392. }
  393. }
  394. efree(bind);
  395. return rc;
  396. }
  397. #else
  398. static int
  399. mysqli_stmt_bind_result_do_bind(MY_STMT *stmt, zval ***args, unsigned int argc, unsigned int start TSRMLS_DC)
  400. {
  401. unsigned int i;
  402. MYSQLND_RESULT_BIND *params;
  403. params = safe_emalloc(argc - start, sizeof(MYSQLND_RESULT_BIND), 0);
  404. for (i = 0; i < (argc - start); i++) {
  405. params[i].zv = *(args[i + start]);
  406. }
  407. return mysqlnd_stmt_bind_result(stmt->stmt, params);
  408. }
  409. #endif
  410. /* }}} */
  411. /* {{{ proto bool mysqli_stmt_bind_result(object stmt, mixed var, [,mixed, ...]) U
  412. Bind variables to a prepared statement for result storage */
  413. PHP_FUNCTION(mysqli_stmt_bind_result)
  414. {
  415. zval ***args;
  416. int argc = ZEND_NUM_ARGS();
  417. int start = 1;
  418. ulong rc;
  419. MY_STMT *stmt;
  420. zval *mysql_stmt;
  421. if (getThis()) {
  422. start = 0;
  423. }
  424. if (zend_parse_method_parameters((getThis()) ? 0:1 TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
  425. return;
  426. }
  427. MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
  428. if (argc < (getThis() ? 1 : 2)) {
  429. WRONG_PARAM_COUNT;
  430. }
  431. if ((argc - start) != mysql_stmt_field_count(stmt->stmt)) {
  432. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number of bind variables doesn't match number of fields in prepared statement");
  433. RETURN_FALSE;
  434. }
  435. args = (zval ***)safe_emalloc(argc, sizeof(zval **), 0);
  436. if (zend_get_parameters_array_ex(argc, args) == FAILURE) {
  437. efree(args);
  438. WRONG_PARAM_COUNT;
  439. }
  440. rc = mysqli_stmt_bind_result_do_bind(stmt, args, argc, start TSRMLS_CC);
  441. efree(args);
  442. RETURN_BOOL(!rc);
  443. }
  444. /* }}} */
  445. /* {{{ proto bool mysqli_change_user(object link, string user, string password, string database)
  446. Change logged-in user of the active connection */
  447. PHP_FUNCTION(mysqli_change_user)
  448. {
  449. MY_MYSQL *mysql;
  450. zval *mysql_link = NULL;
  451. char *user, *password, *dbname;
  452. int user_len, password_len, dbname_len;
  453. ulong rc;
  454. 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) {
  455. return;
  456. }
  457. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
  458. rc = mysql_change_user(mysql->mysql, user, password, dbname);
  459. MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
  460. if (rc) {
  461. RETURN_FALSE;
  462. }
  463. RETURN_TRUE;
  464. }
  465. /* }}} */
  466. /* {{{ proto string mysqli_character_set_name(object link)
  467. Returns the name of the character set used for this connection */
  468. PHP_FUNCTION(mysqli_character_set_name)
  469. {
  470. MY_MYSQL *mysql;
  471. zval *mysql_link;
  472. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
  473. return;
  474. }
  475. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
  476. RETURN_STRING((char *)mysql_character_set_name(mysql->mysql), 1);
  477. }
  478. /* }}} */
  479. /* {{{ proto bool mysqli_close(object link)
  480. Close connection */
  481. PHP_FUNCTION(mysqli_close)
  482. {
  483. zval *mysql_link;
  484. MY_MYSQL *mysql;
  485. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
  486. return;
  487. }
  488. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_INITIALIZED);
  489. if (!mysql->persistent) {
  490. mysqli_close(mysql->mysql, MYSQLI_CLOSE_EXPLICIT);
  491. mysql->mysql = NULL;
  492. } else {
  493. zend_rsrc_list_entry *le;
  494. if (zend_hash_find(&EG(persistent_list), mysql->hash_key, strlen(mysql->hash_key) + 1, (void **)&le) == SUCCESS) {
  495. if (Z_TYPE_P(le) == php_le_pmysqli()) {
  496. mysqli_plist_entry *plist = (mysqli_plist_entry *) le->ptr;
  497. zend_ptr_stack_push(&plist->free_links, mysql->mysql);
  498. MyG(num_links)--;
  499. MyG(num_active_persistent)--;
  500. MyG(num_inactive_persistent)++;
  501. }
  502. }
  503. }
  504. php_clear_mysql(mysql);
  505. MYSQLI_CLEAR_RESOURCE(&mysql_link);
  506. efree(mysql);
  507. RETURN_TRUE;
  508. }
  509. /* }}} */
  510. /* {{{ proto bool mysqli_commit(object link)
  511. Commit outstanding actions and close transaction */
  512. PHP_FUNCTION(mysqli_commit)
  513. {
  514. MY_MYSQL *mysql;
  515. zval *mysql_link;
  516. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
  517. return;
  518. }
  519. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
  520. if (mysql_commit(mysql->mysql)) {
  521. RETURN_FALSE;
  522. }
  523. RETURN_TRUE;
  524. }
  525. /* }}} */
  526. /* {{{ proto bool mysqli_data_seek(object result, int offset)
  527. Move internal result pointer */
  528. PHP_FUNCTION(mysqli_data_seek)
  529. {
  530. MYSQL_RES *result;
  531. zval *mysql_result;
  532. long offset;
  533. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol", &mysql_result, mysqli_result_class_entry, &offset) == FAILURE) {
  534. return;
  535. }
  536. MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
  537. if (mysqli_result_is_unbuffered(result)) {
  538. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Function cannot be used with MYSQL_USE_RESULT");
  539. RETURN_FALSE;
  540. }
  541. if (offset < 0 || offset >= mysql_num_rows(result)) {
  542. RETURN_FALSE;
  543. }
  544. mysql_data_seek(result, offset);
  545. RETURN_TRUE;
  546. }
  547. /* }}} */
  548. /* {{{ proto void mysqli_debug(string debug) U
  549. */
  550. PHP_FUNCTION(mysqli_debug)
  551. {
  552. char *debug;
  553. int debug_len;
  554. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &debug, &debug_len) == FAILURE) {
  555. return;
  556. }
  557. mysql_debug(debug);
  558. RETURN_TRUE;
  559. }
  560. /* }}} */
  561. /* {{{ proto bool mysqli_dump_debug_info(object link)
  562. */
  563. PHP_FUNCTION(mysqli_dump_debug_info)
  564. {
  565. MY_MYSQL *mysql;
  566. zval *mysql_link;
  567. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
  568. return;
  569. }
  570. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
  571. RETURN_BOOL(!mysql_dump_debug_info(mysql->mysql))
  572. }
  573. /* }}} */
  574. /* {{{ proto int mysqli_errno(object link)
  575. Returns the numerical value of the error message from previous MySQL operation */
  576. PHP_FUNCTION(mysqli_errno)
  577. {
  578. MY_MYSQL *mysql;
  579. zval *mysql_link;
  580. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
  581. return;
  582. }
  583. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
  584. RETURN_LONG(mysql_errno(mysql->mysql));
  585. }
  586. /* }}} */
  587. /* {{{ proto string mysqli_error(object link)
  588. Returns the text of the error message from previous MySQL operation */
  589. PHP_FUNCTION(mysqli_error)
  590. {
  591. MY_MYSQL *mysql;
  592. zval *mysql_link;
  593. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
  594. return;
  595. }
  596. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
  597. RETURN_STRING((char *)mysql_error(mysql->mysql),1);
  598. }
  599. /* }}} */
  600. #ifndef MYSQLI_USE_MYSQLND
  601. /* {{{ php_mysqli_stmt_copy_it */
  602. static void
  603. php_mysqli_stmt_copy_it(zval *** copies, zval *original, uint param_count, uint current)
  604. {
  605. if (!*copies) {
  606. *copies = ecalloc(param_count, sizeof(zval *));
  607. }
  608. MAKE_STD_ZVAL((*copies)[current]);
  609. *(*copies)[current] = *original;
  610. Z_SET_REFCOUNT_P((*copies)[current], 1);
  611. zval_copy_ctor((*copies)[current]);
  612. }
  613. /* }}} */
  614. #endif
  615. /* {{{ proto bool mysqli_stmt_execute(object stmt)
  616. Execute a prepared statement */
  617. PHP_FUNCTION(mysqli_stmt_execute)
  618. {
  619. MY_STMT *stmt;
  620. zval *mysql_stmt;
  621. #ifndef MYSQLI_USE_MYSQLND
  622. unsigned int i;
  623. zval **copies = NULL;
  624. #endif
  625. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
  626. return;
  627. }
  628. MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
  629. #ifndef MYSQLI_USE_MYSQLND
  630. if (stmt->param.var_cnt) {
  631. int j;
  632. for (i = 0; i < stmt->param.var_cnt; i++) {
  633. for (j = i + 1; j < stmt->param.var_cnt; j++) {
  634. /* Oops, someone binding the same variable - clone */
  635. if (stmt->param.vars[j] == stmt->param.vars[i]) {
  636. php_mysqli_stmt_copy_it(&copies, stmt->param.vars[i], stmt->param.var_cnt, i);
  637. break;
  638. }
  639. }
  640. }
  641. }
  642. for (i = 0; i < stmt->param.var_cnt; i++) {
  643. if (stmt->param.vars[i]) {
  644. if ( !(stmt->param.is_null[i] = (stmt->param.vars[i]->type == IS_NULL)) ) {
  645. zval *the_var = copies && copies[i]? copies[i]:stmt->param.vars[i];
  646. switch (stmt->stmt->params[i].buffer_type) {
  647. case MYSQL_TYPE_VAR_STRING:
  648. if (the_var == stmt->param.vars[i] && Z_TYPE_P(stmt->param.vars[i]) != IS_STRING) {
  649. php_mysqli_stmt_copy_it(&copies, stmt->param.vars[i], stmt->param.var_cnt, i);
  650. the_var = copies[i];
  651. }
  652. convert_to_string_ex(&the_var);
  653. stmt->stmt->params[i].buffer = Z_STRVAL_P(the_var);
  654. stmt->stmt->params[i].buffer_length = Z_STRLEN_P(the_var);
  655. break;
  656. case MYSQL_TYPE_DOUBLE:
  657. if (the_var == stmt->param.vars[i] && Z_TYPE_P(stmt->param.vars[i]) != IS_DOUBLE) {
  658. php_mysqli_stmt_copy_it(&copies, stmt->param.vars[i], stmt->param.var_cnt, i);
  659. the_var = copies[i];
  660. }
  661. convert_to_double_ex(&the_var);
  662. stmt->stmt->params[i].buffer = &Z_DVAL_P(the_var);
  663. break;
  664. case MYSQL_TYPE_LONGLONG:
  665. case MYSQL_TYPE_LONG:
  666. if (the_var == stmt->param.vars[i] && Z_TYPE_P(stmt->param.vars[i]) != IS_LONG) {
  667. php_mysqli_stmt_copy_it(&copies, stmt->param.vars[i], stmt->param.var_cnt, i);
  668. the_var = copies[i];
  669. }
  670. convert_to_long_ex(&the_var);
  671. stmt->stmt->params[i].buffer = &Z_LVAL_P(the_var);
  672. break;
  673. default:
  674. break;
  675. }
  676. }
  677. }
  678. }
  679. #endif
  680. if (mysql_stmt_execute(stmt->stmt)) {
  681. MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
  682. RETVAL_FALSE;
  683. } else {
  684. RETVAL_TRUE;
  685. }
  686. #ifndef MYSQLI_USE_MYSQLND
  687. if (copies) {
  688. for (i = 0; i < stmt->param.var_cnt; i++) {
  689. if (copies[i]) {
  690. zval_ptr_dtor(&copies[i]);
  691. }
  692. }
  693. efree(copies);
  694. }
  695. #endif
  696. if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
  697. php_mysqli_report_index(stmt->query, mysqli_stmt_server_status(stmt->stmt) TSRMLS_CC);
  698. }
  699. }
  700. /* }}} */
  701. #ifndef MYSQLI_USE_MYSQLND
  702. /* {{{ void mysqli_stmt_fetch_libmysql
  703. Fetch results from a prepared statement into the bound variables */
  704. void mysqli_stmt_fetch_libmysql(INTERNAL_FUNCTION_PARAMETERS)
  705. {
  706. MY_STMT *stmt;
  707. zval *mysql_stmt;
  708. unsigned int i;
  709. ulong ret;
  710. unsigned int uval;
  711. my_ulonglong llval;
  712. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
  713. return;
  714. }
  715. MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
  716. /* reset buffers */
  717. for (i = 0; i < stmt->result.var_cnt; i++) {
  718. if (stmt->result.buf[i].type == IS_STRING) {
  719. memset(stmt->result.buf[i].val, 0, stmt->result.buf[i].buflen);
  720. }
  721. }
  722. ret = mysql_stmt_fetch(stmt->stmt);
  723. #ifdef MYSQL_DATA_TRUNCATED
  724. if (!ret || ret == MYSQL_DATA_TRUNCATED) {
  725. #else
  726. if (!ret) {
  727. #endif
  728. for (i = 0; i < stmt->result.var_cnt; i++) {
  729. /*
  730. QQ: Isn't it quite better to call zval_dtor(). What if the user has
  731. assigned a resource, or an array to the bound variable? We are going
  732. to leak probably. zval_dtor() will handle also Unicode/Non-unicode mode.
  733. */
  734. /* Even if the string is of length zero there is one byte alloced so efree() in all cases */
  735. if (Z_TYPE_P(stmt->result.vars[i]) == IS_STRING) {
  736. efree(stmt->result.vars[i]->value.str.val);
  737. }
  738. if (!stmt->result.is_null[i]) {
  739. switch (stmt->result.buf[i].type) {
  740. case IS_LONG:
  741. if ((stmt->stmt->fields[i].type == MYSQL_TYPE_LONG)
  742. && (stmt->stmt->fields[i].flags & UNSIGNED_FLAG))
  743. {
  744. /* unsigned int (11) */
  745. uval= *(unsigned int *) stmt->result.buf[i].val;
  746. #if SIZEOF_LONG==4
  747. if (uval > INT_MAX) {
  748. char *tmp, *p;
  749. int j=10;
  750. tmp= emalloc(11);
  751. p= &tmp[9];
  752. do {
  753. *p-- = (uval % 10) + 48;
  754. uval = uval / 10;
  755. } while (--j > 0);
  756. tmp[10]= '\0';
  757. /* unsigned int > INT_MAX is 10 digits - ALWAYS */
  758. ZVAL_STRINGL(stmt->result.vars[i], tmp, 10, 0);
  759. break;
  760. }
  761. #endif
  762. }
  763. if (stmt->stmt->fields[i].flags & UNSIGNED_FLAG) {
  764. ZVAL_LONG(stmt->result.vars[i], *(unsigned int *)stmt->result.buf[i].val);
  765. } else {
  766. ZVAL_LONG(stmt->result.vars[i], *(int *)stmt->result.buf[i].val);
  767. }
  768. break;
  769. case IS_DOUBLE:
  770. ZVAL_DOUBLE(stmt->result.vars[i], *(double *)stmt->result.buf[i].val);
  771. break;
  772. case IS_STRING:
  773. if (stmt->stmt->bind[i].buffer_type == MYSQL_TYPE_LONGLONG) {
  774. my_bool uns= (stmt->stmt->fields[i].flags & UNSIGNED_FLAG)? 1:0;
  775. llval= *(my_ulonglong *) stmt->result.buf[i].val;
  776. #if SIZEOF_LONG==8
  777. if (uns && llval > 9223372036854775807L) {
  778. #elif SIZEOF_LONG==4
  779. if ((uns && llval > L64(2147483647)) ||
  780. (!uns && (( L64(2147483647) < (my_longlong) llval) ||
  781. (L64(-2147483648) > (my_longlong) llval))))
  782. {
  783. #endif
  784. char tmp[22];
  785. /* even though lval is declared as unsigned, the value
  786. * may be negative. Therefor we cannot use MYSQLI_LLU_SPEC and must
  787. * use MYSQLI_LL_SPEC.
  788. */
  789. snprintf(tmp, sizeof(tmp), (stmt->stmt->fields[i].flags & UNSIGNED_FLAG)? MYSQLI_LLU_SPEC : MYSQLI_LL_SPEC, llval);
  790. ZVAL_STRING(stmt->result.vars[i], tmp, 1);
  791. } else {
  792. ZVAL_LONG(stmt->result.vars[i], llval);
  793. }
  794. }
  795. #if MYSQL_VERSION_ID > 50002
  796. else if (stmt->stmt->bind[i].buffer_type == MYSQL_TYPE_BIT) {
  797. llval = *(my_ulonglong *)stmt->result.buf[i].val;
  798. ZVAL_LONG(stmt->result.vars[i], llval);
  799. }
  800. #endif
  801. else {
  802. #if defined(MYSQL_DATA_TRUNCATED) && MYSQL_VERSION_ID > 50002
  803. if (ret == MYSQL_DATA_TRUNCATED && *(stmt->stmt->bind[i].error) != 0) {
  804. /* result was truncated */
  805. ZVAL_STRINGL(stmt->result.vars[i], stmt->result.buf[i].val,
  806. stmt->stmt->bind[i].buffer_length, 1);
  807. } else {
  808. #else
  809. {
  810. #endif
  811. ZVAL_STRINGL(stmt->result.vars[i], stmt->result.buf[i].val,
  812. stmt->result.buf[i].buflen, 1);
  813. }
  814. }
  815. break;
  816. default:
  817. break;
  818. }
  819. } else {
  820. ZVAL_NULL(stmt->result.vars[i]);
  821. }
  822. }
  823. } else {
  824. MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
  825. }
  826. switch (ret) {
  827. case 0:
  828. #ifdef MYSQL_DATA_TRUNCATED
  829. /* according to SQL standard truncation (e.g. loss of precision is
  830. not an error) - for detecting possible truncation you have to
  831. check mysqli_stmt_warning
  832. */
  833. case MYSQL_DATA_TRUNCATED:
  834. #endif
  835. RETURN_TRUE;
  836. break;
  837. case 1:
  838. RETURN_FALSE;
  839. break;
  840. default:
  841. RETURN_NULL();
  842. break;
  843. }
  844. }
  845. /* }}} */
  846. #else
  847. /* {{{ mixed mysqli_stmt_fetch_mysqlnd */
  848. void mysqli_stmt_fetch_mysqlnd(INTERNAL_FUNCTION_PARAMETERS)
  849. {
  850. MY_STMT *stmt;
  851. zval *mysql_stmt;
  852. zend_bool fetched_anything;
  853. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
  854. return;
  855. }
  856. MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
  857. if (FAIL == mysqlnd_stmt_fetch(stmt->stmt, &fetched_anything)) {
  858. RETURN_BOOL(FALSE);
  859. } else if (fetched_anything == TRUE) {
  860. RETURN_BOOL(TRUE);
  861. } else {
  862. RETURN_NULL();
  863. }
  864. }
  865. #endif
  866. /* }}} */
  867. /* {{{ proto mixed mysqli_stmt_fetch(object stmt) U
  868. Fetch results from a prepared statement into the bound variables */
  869. PHP_FUNCTION(mysqli_stmt_fetch)
  870. {
  871. #if !defined(MYSQLI_USE_MYSQLND)
  872. mysqli_stmt_fetch_libmysql(INTERNAL_FUNCTION_PARAM_PASSTHRU);
  873. #else
  874. mysqli_stmt_fetch_mysqlnd(INTERNAL_FUNCTION_PARAM_PASSTHRU);
  875. #endif
  876. }
  877. /* }}} */
  878. /* {{{ php_add_field_properties */
  879. static void php_add_field_properties(zval *value, const MYSQL_FIELD *field TSRMLS_DC)
  880. {
  881. add_property_string(value, "name",(field->name ? field->name : ""), 1);
  882. add_property_string(value, "orgname",(field->org_name ? field->org_name : ""), 1);
  883. add_property_string(value, "table",(field->table ? field->table : ""), 1);
  884. add_property_string(value, "orgtable",(field->org_table ? field->org_table : ""), 1);
  885. add_property_string(value, "def",(field->def ? field->def : ""), 1);
  886. add_property_long(value, "max_length", field->max_length);
  887. add_property_long(value, "length", field->length);
  888. add_property_long(value, "charsetnr", field->charsetnr);
  889. add_property_long(value, "flags", field->flags);
  890. add_property_long(value, "type", field->type);
  891. add_property_long(value, "decimals", field->decimals);
  892. }
  893. /* }}} */
  894. /* {{{ proto mixed mysqli_fetch_field (object result)
  895. Get column information from a result and return as an object */
  896. PHP_FUNCTION(mysqli_fetch_field)
  897. {
  898. MYSQL_RES *result;
  899. zval *mysql_result;
  900. const MYSQL_FIELD *field;
  901. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
  902. return;
  903. }
  904. MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
  905. if (!(field = mysql_fetch_field(result))) {
  906. RETURN_FALSE;
  907. }
  908. object_init(return_value);
  909. php_add_field_properties(return_value, field TSRMLS_CC);
  910. }
  911. /* }}} */
  912. /* {{{ proto mixed mysqli_fetch_fields (object result)
  913. Return array of objects containing field meta-data */
  914. PHP_FUNCTION(mysqli_fetch_fields)
  915. {
  916. MYSQL_RES *result;
  917. zval *mysql_result;
  918. zval *obj;
  919. unsigned int i;
  920. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
  921. return;
  922. }
  923. MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
  924. array_init(return_value);
  925. for (i = 0; i < mysql_num_fields(result); i++) {
  926. const MYSQL_FIELD *field = mysql_fetch_field_direct(result, i);
  927. MAKE_STD_ZVAL(obj);
  928. object_init(obj);
  929. php_add_field_properties(obj, field TSRMLS_CC);
  930. add_index_zval(return_value, i, obj);
  931. }
  932. }
  933. /* }}} */
  934. /* {{{ proto mixed mysqli_fetch_field_direct (object result, int offset)
  935. Fetch meta-data for a single field */
  936. PHP_FUNCTION(mysqli_fetch_field_direct)
  937. {
  938. MYSQL_RES *result;
  939. zval *mysql_result;
  940. const MYSQL_FIELD *field;
  941. long offset;
  942. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol", &mysql_result, mysqli_result_class_entry, &offset) == FAILURE) {
  943. return;
  944. }
  945. MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
  946. if (offset < 0 || offset >= mysql_num_fields(result)) {
  947. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Field offset is invalid for resultset");
  948. RETURN_FALSE;
  949. }
  950. if (!(field = mysql_fetch_field_direct(result,offset))) {
  951. RETURN_FALSE;
  952. }
  953. object_init(return_value);
  954. php_add_field_properties(return_value, field TSRMLS_CC);
  955. }
  956. /* }}} */
  957. /* {{{ proto mixed mysqli_fetch_lengths (object result)
  958. Get the length of each output in a result */
  959. PHP_FUNCTION(mysqli_fetch_lengths)
  960. {
  961. MYSQL_RES *result;
  962. zval *mysql_result;
  963. unsigned int i;
  964. unsigned long *ret;
  965. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
  966. return;
  967. }
  968. MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
  969. if (!(ret = mysql_fetch_lengths(result))) {
  970. RETURN_FALSE;
  971. }
  972. array_init(return_value);
  973. for (i = 0; i < mysql_num_fields(result); i++) {
  974. add_index_long(return_value, i, ret[i]);
  975. }
  976. }
  977. /* }}} */
  978. /* {{{ proto array mysqli_fetch_row (object result)
  979. Get a result row as an enumerated array */
  980. PHP_FUNCTION(mysqli_fetch_row)
  981. {
  982. #if !defined(MYSQLI_USE_MYSQLND)
  983. php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQLI_NUM, 0);
  984. #else
  985. MYSQL_RES *result;
  986. zval *mysql_result;
  987. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
  988. return;
  989. }
  990. MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
  991. mysqlnd_fetch_into(result, MYSQLND_FETCH_NUM, return_value, MYSQLND_MYSQLI);
  992. #endif
  993. }
  994. /* }}} */
  995. /* {{{ proto int mysqli_field_count(object link)
  996. Fetch the number of fields returned by the last query for the given link
  997. */
  998. PHP_FUNCTION(mysqli_field_count)
  999. {
  1000. MY_MYSQL *mysql;
  1001. zval *mysql_link;
  1002. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
  1003. return;
  1004. }
  1005. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
  1006. RETURN_LONG(mysql_field_count(mysql->mysql));
  1007. }
  1008. /* }}} */
  1009. /* {{{ proto int mysqli_field_seek(object result, int fieldnr)
  1010. Set result pointer to a specified field offset
  1011. */
  1012. PHP_FUNCTION(mysqli_field_seek)
  1013. {
  1014. MYSQL_RES *result;
  1015. zval *mysql_result;
  1016. unsigned long fieldnr;
  1017. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol", &mysql_result, mysqli_result_class_entry, &fieldnr) == FAILURE) {
  1018. return;
  1019. }
  1020. MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
  1021. if (fieldnr < 0 || fieldnr >= mysql_num_fields(result)) {
  1022. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid field offset");
  1023. RETURN_FALSE;
  1024. }
  1025. mysql_field_seek(result, fieldnr);
  1026. RETURN_TRUE;
  1027. }
  1028. /* }}} */
  1029. /* {{{ proto int mysqli_field_tell(object result)
  1030. Get current field offset of result pointer */
  1031. PHP_FUNCTION(mysqli_field_tell)
  1032. {
  1033. MYSQL_RES *result;
  1034. zval *mysql_result;
  1035. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
  1036. return;
  1037. }
  1038. MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
  1039. RETURN_LONG(mysql_field_tell(result));
  1040. }
  1041. /* }}} */
  1042. /* {{{ proto void mysqli_free_result(object result)
  1043. Free query result memory for the given result handle */
  1044. PHP_FUNCTION(mysqli_free_result)
  1045. {
  1046. MYSQL_RES *result;
  1047. zval *mysql_result;
  1048. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
  1049. return;
  1050. }
  1051. MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
  1052. mysqli_free_result(result, FALSE);
  1053. MYSQLI_CLEAR_RESOURCE(&mysql_result);
  1054. }
  1055. /* }}} */
  1056. /* {{{ proto string mysqli_get_client_info(void)
  1057. Get MySQL client info */
  1058. PHP_FUNCTION(mysqli_get_client_info)
  1059. {
  1060. RETURN_STRING((char *)mysql_get_client_info(), 1);
  1061. }
  1062. /* }}} */
  1063. /* {{{ proto int mysqli_get_client_version(void)
  1064. Get MySQL client info */
  1065. PHP_FUNCTION(mysqli_get_client_version)
  1066. {
  1067. RETURN_LONG((long)mysql_get_client_version());
  1068. }
  1069. /* }}} */
  1070. /* {{{ proto string mysqli_get_host_info (object link)
  1071. Get MySQL host info */
  1072. PHP_FUNCTION(mysqli_get_host_info)
  1073. {
  1074. MY_MYSQL *mysql;
  1075. zval *mysql_link = NULL;
  1076. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
  1077. return;
  1078. }
  1079. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
  1080. RETURN_STRING((mysql->mysql->host_info) ? mysql->mysql->host_info : "", 1);
  1081. }
  1082. /* }}} */
  1083. /* {{{ proto int mysqli_get_proto_info(object link)
  1084. Get MySQL protocol information */
  1085. PHP_FUNCTION(mysqli_get_proto_info)
  1086. {
  1087. MY_MYSQL *mysql;
  1088. zval *mysql_link = NULL;
  1089. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
  1090. return;
  1091. }
  1092. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
  1093. RETURN_LONG(mysql_get_proto_info(mysql->mysql));
  1094. }
  1095. /* }}} */
  1096. /* {{{ proto string mysqli_get_server_info(object link)
  1097. Get MySQL server info */
  1098. PHP_FUNCTION(mysqli_get_server_info)
  1099. {
  1100. MY_MYSQL *mysql;
  1101. zval *mysql_link = NULL;
  1102. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
  1103. return;
  1104. }
  1105. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
  1106. RETURN_STRING((char *)mysql_get_server_info(mysql->mysql), 1);
  1107. }
  1108. /* }}} */
  1109. /* {{{ proto int mysqli_get_server_version(object link)
  1110. Return the MySQL version for the server referenced by the given link */
  1111. PHP_FUNCTION(mysqli_get_server_version)
  1112. {
  1113. MY_MYSQL *mysql;
  1114. zval *mysql_link = NULL;
  1115. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
  1116. return;
  1117. }
  1118. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
  1119. RETURN_LONG(mysql_get_server_version(mysql->mysql));
  1120. }
  1121. /* }}} */
  1122. /* {{{ proto string mysqli_info(object link)
  1123. Get information about the most recent query */
  1124. PHP_FUNCTION(mysqli_info)
  1125. {
  1126. MY_MYSQL *mysql;
  1127. zval *mysql_link = NULL;
  1128. const char *info;
  1129. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
  1130. return;
  1131. }
  1132. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
  1133. info = mysql_info(mysql->mysql);
  1134. RETURN_STRING((info) ? (char *)info : "", 1);
  1135. }
  1136. /* }}} */
  1137. /* {{{ proto resource mysqli_init(void)
  1138. Initialize mysqli and return a resource for use with mysql_real_connect */
  1139. PHP_FUNCTION(mysqli_init)
  1140. {
  1141. MYSQLI_RESOURCE *mysqli_resource;
  1142. MY_MYSQL *mysql;
  1143. if (getThis() && ((mysqli_object *) zend_object_store_get_object(getThis() TSRMLS_CC))->ptr) {
  1144. return;
  1145. }
  1146. mysql = (MY_MYSQL *)ecalloc(1, sizeof(MY_MYSQL));
  1147. #if !defined(MYSQLI_USE_MYSQLND)
  1148. if (!(mysql->mysql = mysql_init(NULL)))
  1149. #else
  1150. /*
  1151. We create always persistent, as if the user want to connecto
  1152. to p:somehost, we can't convert the handle then
  1153. */
  1154. if (!(mysql->mysql = mysql_init(TRUE)))
  1155. #endif
  1156. {
  1157. efree(mysql);
  1158. RETURN_FALSE;
  1159. }
  1160. mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
  1161. mysqli_resource->ptr = (void *)mysql;
  1162. mysqli_resource->status = MYSQLI_STATUS_INITIALIZED;
  1163. if (!getThis() || !instanceof_function(Z_OBJCE_P(getThis()), mysqli_link_class_entry TSRMLS_CC)) {
  1164. MYSQLI_RETURN_RESOURCE(mysqli_resource, mysqli_link_class_entry);
  1165. } else {
  1166. ((mysqli_object *) zend_object_store_get_object(getThis() TSRMLS_CC))->ptr = mysqli_resource;
  1167. }
  1168. }
  1169. /* }}} */
  1170. /* {{{ proto mixed mysqli_insert_id(object link)
  1171. Get the ID generated from the previous INSERT operation */
  1172. PHP_FUNCTION(mysqli_insert_id)
  1173. {
  1174. MY_MYSQL *mysql;
  1175. my_ulonglong rc;
  1176. zval *mysql_link;
  1177. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
  1178. return;
  1179. }
  1180. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
  1181. rc = mysql_insert_id(mysql->mysql);
  1182. MYSQLI_RETURN_LONG_LONG(rc)
  1183. }
  1184. /* }}} */
  1185. /* {{{ proto bool mysqli_kill(object link, int processid)
  1186. Kill a mysql process on the server */
  1187. PHP_FUNCTION(mysqli_kill)
  1188. {
  1189. MY_MYSQL *mysql;
  1190. zval *mysql_link;
  1191. long processid;
  1192. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol", &mysql_link, mysqli_link_class_entry, &processid) == FAILURE) {
  1193. return;
  1194. }
  1195. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
  1196. if (processid <= 0) {
  1197. php_error_docref(NULL TSRMLS_CC, E_WARNING, "processid should have positive value");
  1198. RETURN_FALSE;
  1199. }
  1200. if (mysql_kill(mysql->mysql, processid)) {
  1201. MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
  1202. RETURN_FALSE;
  1203. }
  1204. RETURN_TRUE;
  1205. }
  1206. /* }}} */
  1207. /* {{{ proto void mysqli_set_local_infile_default(object link)
  1208. unsets user defined handler for load local infile command */
  1209. #if !defined(MYSQLI_USE_MYSQLND)
  1210. PHP_FUNCTION(mysqli_set_local_infile_default)
  1211. {
  1212. MY_MYSQL *mysql;
  1213. zval *mysql_link;
  1214. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
  1215. return;
  1216. }
  1217. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
  1218. if (mysql->li_read) {
  1219. zval_ptr_dtor(&(mysql->li_read));
  1220. mysql->li_read = NULL;
  1221. }
  1222. }
  1223. /* }}} */
  1224. /* {{{ proto bool mysqli_set_local_infile_handler(object link, callback read_func)
  1225. Set callback functions for LOAD DATA LOCAL INFILE */
  1226. PHP_FUNCTION(mysqli_set_local_infile_handler)
  1227. {
  1228. MY_MYSQL *mysql;
  1229. zval *mysql_link;
  1230. char *callback_name;
  1231. zval *callback_func;
  1232. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oz", &mysql_link, mysqli_link_class_entry,
  1233. &callback_func) == FAILURE) {
  1234. return;
  1235. }
  1236. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
  1237. /* check callback function */
  1238. if (!zend_is_callable(callback_func, 0, &callback_name TSRMLS_CC)) {
  1239. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not a valid callback function %s", callback_name);
  1240. efree(callback_name);
  1241. RETURN_FALSE;
  1242. }
  1243. efree(callback_name);
  1244. /* save callback function */
  1245. if (!mysql->li_read) {
  1246. MAKE_STD_ZVAL(mysql->li_read);
  1247. } else {
  1248. zval_dtor(mysql->li_read);
  1249. }
  1250. ZVAL_ZVAL(mysql->li_read, callback_func, 1, 0);
  1251. RETURN_TRUE;
  1252. }
  1253. #endif
  1254. /* }}} */
  1255. /* {{{ proto bool mysqli_more_results(object link)
  1256. check if there any more query results from a multi query */
  1257. PHP_FUNCTION(mysqli_more_results)
  1258. {
  1259. MY_MYSQL *mysql;
  1260. zval *mysql_link;
  1261. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
  1262. return;
  1263. }
  1264. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
  1265. RETURN_BOOL(mysql_more_results(mysql->mysql));
  1266. }
  1267. /* }}} */
  1268. /* {{{ proto bool mysqli_next_result(object link)
  1269. read next result from multi_query */
  1270. PHP_FUNCTION(mysqli_next_result) {
  1271. MY_MYSQL *mysql;
  1272. zval *mysql_link;
  1273. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
  1274. return;
  1275. }
  1276. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
  1277. if (!mysql_more_results(mysql->mysql)) {
  1278. php_error_docref(NULL TSRMLS_CC, E_STRICT, "There is no next result set. "
  1279. "Please, call mysqli_more_results()/mysqli::more_results() to check "
  1280. "whether to call this function/method");
  1281. }
  1282. RETURN_BOOL(!mysql_next_result(mysql->mysql));
  1283. }
  1284. /* }}} */
  1285. #ifdef MYSQLI_USE_MYSQLND
  1286. /* {{{ proto bool mysqli_stmt_next_result(object link)
  1287. check if there any more query results from a multi query */
  1288. PHP_FUNCTION(mysqli_stmt_more_results)
  1289. {
  1290. MY_STMT *stmt;
  1291. zval *mysql_stmt;
  1292. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
  1293. return;
  1294. }
  1295. MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
  1296. RETURN_BOOL(mysqlnd_stmt_more_results(stmt->stmt));
  1297. }
  1298. /* }}} */
  1299. /* {{{ proto bool mysqli_stmt_next_result(object link)
  1300. read next result from multi_query */
  1301. PHP_FUNCTION(mysqli_stmt_next_result) {
  1302. MY_STMT *stmt;
  1303. zval *mysql_stmt;
  1304. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
  1305. return;
  1306. }
  1307. MYSQLI_FETCH_RESOURCE(stmt, MY_STMT *, &mysql_stmt, "mysqli_stmt", MYSQLI_STATUS_VALID);
  1308. if (!mysqlnd_stmt_more_results(stmt->stmt)) {
  1309. php_error_docref(NULL TSRMLS_CC, E_STRICT, "There is no next result set. "
  1310. "Please, call mysqli_stmt_more_results()/mysqli_stmt::more_results() to check "
  1311. "whether to call this function/method");
  1312. }
  1313. RETURN_BOOL(!mysqlnd_stmt_next_result(stmt->stmt));
  1314. }
  1315. /* }}} */
  1316. #endif
  1317. /* {{{ proto int mysqli_num_fields(object result)
  1318. Get number of fields in result */
  1319. PHP_FUNCTION(mysqli_num_fields)
  1320. {
  1321. MYSQL_RES *result;
  1322. zval *mysql_result;
  1323. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
  1324. return;
  1325. }
  1326. MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
  1327. RETURN_LONG(mysql_num_fields(result));
  1328. }
  1329. /* }}} */
  1330. /* {{{ proto mixed mysqli_num_rows(object result)
  1331. Get number of rows in result */
  1332. PHP_FUNCTION(mysqli_num_rows)
  1333. {
  1334. MYSQL_RES *result;
  1335. zval *mysql_result;
  1336. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
  1337. return;
  1338. }
  1339. MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, &mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
  1340. if (mysqli_result_is_unbuffered(result)) {
  1341. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Function cannot be used with MYSQL_USE_RESULT");
  1342. RETURN_LONG(0);
  1343. }
  1344. MYSQLI_RETURN_LONG_LONG(mysql_num_rows(result));
  1345. }
  1346. /* }}} */
  1347. /* {{{ proto bool mysqli_options(object link, int flags, mixed values)
  1348. Set options */
  1349. PHP_FUNCTION(mysqli_options)
  1350. {
  1351. MY_MYSQL *mysql;
  1352. zval *mysql_link = NULL;
  1353. zval *mysql_value;
  1354. long mysql_option;
  1355. unsigned int l_value;
  1356. long ret;
  1357. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Olz", &mysql_link, mysqli_link_class_entry, &mysql_option, &mysql_value) == FAILURE) {
  1358. return;
  1359. }
  1360. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_INITIALIZED);
  1361. if ((PG(open_basedir) && PG(open_basedir)[0] != '\0') || PG(safe_mode)) {
  1362. if(mysql_option == MYSQL_OPT_LOCAL_INFILE) {
  1363. RETURN_FALSE;
  1364. }
  1365. }
  1366. switch (Z_TYPE_PP(&mysql_value)) {
  1367. case IS_STRING:
  1368. ret = mysql_options(mysql->mysql, mysql_option, Z_STRVAL_PP(&mysql_value));
  1369. break;
  1370. default:
  1371. convert_to_long_ex(&mysql_value);
  1372. l_value = Z_LVAL_PP(&mysql_value);
  1373. ret = mysql_options(mysql->mysql, mysql_option, (char *)&l_value);
  1374. break;
  1375. }
  1376. RETURN_BOOL(!ret);
  1377. }
  1378. /* }}} */
  1379. /* {{{ proto bool mysqli_ping(object link)
  1380. Ping a server connection or reconnect if there is no connection */
  1381. PHP_FUNCTION(mysqli_ping)
  1382. {
  1383. MY_MYSQL *mysql;
  1384. zval *mysql_link;
  1385. long rc;
  1386. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
  1387. return;
  1388. }
  1389. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
  1390. rc = mysql_ping(mysql->mysql);
  1391. MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
  1392. RETURN_BOOL(!rc);
  1393. }
  1394. /* }}} */
  1395. /* {{{ proto mixed mysqli_prepare(object link, string query)
  1396. Prepare a SQL statement for execution */
  1397. PHP_FUNCTION(mysqli_prepare)
  1398. {
  1399. MY_MYSQL *mysql;
  1400. MY_STMT *stmt;
  1401. char *query = NULL;
  1402. unsigned int query_len;
  1403. zval *mysql_link;
  1404. MYSQLI_RESOURCE *mysqli_resource;
  1405. if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",&mysql_link, mysqli_link_class_entry, &query, &query_len) == FAILURE) {
  1406. return;
  1407. }
  1408. MYSQLI_FETCH_RESOURCE(mysql, MY_MYSQL *, &mysql_link, "mysqli_link", MYSQLI_STATUS_VALID);
  1409. #if !defined(MYSQLI_USE_MYSQLND)
  1410. if (mysql->mysql->status == MYSQL_STATUS_GET_RESULT) {
  1411. php_error_docref(NULL TSRMLS_CC, E_WARNING, "All data must be fetched before a new statement prepare takes place");
  1412. RETURN_FALSE;
  1413. }
  1414. #endif
  1415. stmt = (MY_STMT *)ecalloc(1,sizeof(MY_STMT));
  1416. if ((stmt->stmt = mysql_stmt_init(mysql->mysql))) {
  1417. if (mysql_stmt_prepare(stmt->stmt, query, query_len)) {
  1418. /* mysql_stmt_close() clears errors, so we have to store them temporarily */
  1419. #if !defined(MYSQLI_USE_MYSQLND)
  1420. char last_error[MYSQL_ERRMSG_SIZE];
  1421. char sqlstate[SQLSTATE_LENGTH+1];
  1422. unsigned int last_errno;
  1423. last_errno = stmt->stmt->last_errno;
  1424. memcpy(last_error, stmt->stmt->last_error, MYSQL_ERRMSG_SIZE);
  1425. memcpy(sqlstate, mysql->mysql->net.sqlstate, SQLSTATE_LENGTH+1);
  1426. #else
  1427. mysqlnd_error_info error_info = mysql->mysql->error_info;
  1428. #endif
  1429. mysqli_stmt_close(stmt->stmt, FALSE);
  1430. stmt->stmt = NULL;
  1431. /* restore error messages */
  1432. #if !defined(MYSQLI_USE_MYSQLND)
  1433. mysql->mysql->net.last_errno = last_errno;
  1434. memcpy(mysql->mysql->net.last_error, last_error, MYSQL_ERRMSG_SIZE);
  1435. memcpy(mysql->mysql->net.sqlstate, sqlstate, SQLSTATE_LENGTH+1);
  1436. #else
  1437. mysql->mysql->error_info = error_info;
  1438. #endif
  1439. }
  1440. }
  1441. /* don't initialize stmt->query with NULL, we ecalloc()-ed the memory */
  1442. /* Get performance boost if reporting is switched off */
  1443. if (stmt->stmt && query_len && (MyG(report_mode) & MYSQLI_REPORT_INDEX)) {
  1444. stmt->query = (char *)emalloc(query_len + 1);
  1445. memcpy(stmt->query, query, query_len);
  1446. stmt->query[query_len] = '\0';
  1447. }
  1448. /* don't join to the previous if because it won't work if mysql_stmt_prepare_fails */
  1449. if (!stmt->stmt) {
  1450. MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
  1451. efree(stmt);
  1452. RETURN_FALSE;
  1453. }
  1454. mysqli_resource = (MYSQLI_R

Large files files are truncated, but you can click here to view the full file