PageRenderTime 68ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/mysqli/mysqli_api.c

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