mysqli_options.phpt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. --TEST--
  2. mysqli_options()
  3. --EXTENSIONS--
  4. mysqli
  5. --SKIPIF--
  6. <?php
  7. require_once 'skipifconnectfailure.inc';
  8. ?>
  9. --FILE--
  10. <?php
  11. require_once "connect.inc";
  12. $link = mysqli_init();
  13. /* set it twice, checking if memory for the previous one is correctly freed */
  14. mysqli_options($link, MYSQLI_SET_CHARSET_NAME, "utf8");
  15. mysqli_options($link, MYSQLI_SET_CHARSET_NAME, "latin1");
  16. // print "run_tests.php don't fool me with your 'ungreedy' expression '.+?'!\n";
  17. var_dump("MYSQLI_READ_DEFAULT_GROUP", mysqli_options($link, MYSQLI_READ_DEFAULT_GROUP, 'extra_my.cnf'));
  18. var_dump("MYSQLI_READ_DEFAULT_FILE", mysqli_options($link, MYSQLI_READ_DEFAULT_FILE, 'extra_my.cnf'));
  19. var_dump("MYSQLI_OPT_CONNECT_TIMEOUT", mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 10));
  20. var_dump("MYSQLI_OPT_LOCAL_INFILE", mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, 1));
  21. var_dump("MYSQLI_INIT_COMMAND", mysqli_options($link, MYSQLI_INIT_COMMAND, array('SET AUTOCOMMIT=0', 'SET AUTOCOMMIT=1')));
  22. if (!$link2 = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
  23. printf("[006] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
  24. $host, $user, $db, $port, $socket);
  25. }
  26. if (!$res = mysqli_query($link2, 'SELECT version() AS server_version')) {
  27. printf("[007] [%d] %s\n", mysqli_errno($link2), mysqli_error($link2));
  28. }
  29. $tmp = mysqli_fetch_assoc($res);
  30. mysqli_free_result($res);
  31. $version = explode('.', $tmp['server_version']);
  32. if (empty($version)) {
  33. printf("[008] Cannot determine server version, need MySQL Server 4.1+ for the test!\n");
  34. }
  35. if ($version[0] <= 4 && $version[1] < 1) {
  36. printf("[009] Need MySQL Server 4.1+ for the test!\n");
  37. }
  38. if (!$res = mysqli_query($link2, "SHOW CHARACTER SET")) {
  39. printf("[010] Cannot get list of available character sets, [%d] %s\n",
  40. mysqli_errno($link2), mysqli_error($link2));
  41. }
  42. $charsets = array();
  43. while ($row = mysqli_fetch_assoc($res)) {
  44. $charsets[] = $row;
  45. }
  46. mysqli_free_result($res);
  47. mysqli_close($link2);
  48. foreach ($charsets as $charset) {
  49. /* The server currently 17.07.2007 can't handle data sent in ucs2 */
  50. /* The server currently 16.08.2010 can't handle data sent in utf16 and utf32 */
  51. /* As of MySQL 8.0.28, `SHOW CHARACTER SET` contains utf8mb3, but that is not yet supported by mysqlnd */
  52. if ($charset['Charset'] == 'ucs2' || $charset['Charset'] == 'utf16' || $charset['Charset'] == 'utf32' || $charset['Charset'] == 'utf8mb3') {
  53. continue;
  54. }
  55. if (true !== mysqli_options($link, MYSQLI_SET_CHARSET_NAME, $charset['Charset'])) {
  56. printf("[009] Setting charset name '%s' has failed\n", $charset['Charset']);
  57. }
  58. }
  59. var_dump("MYSQLI_READ_DEFAULT_GROUP", mysqli_options($link, MYSQLI_READ_DEFAULT_GROUP, 'extra_my.cnf'));
  60. var_dump("MYSQLI_READ_DEFAULT_FILE", mysqli_options($link, MYSQLI_READ_DEFAULT_FILE, 'extra_my.cnf'));
  61. var_dump("MYSQLI_OPT_CONNECT_TIMEOUT", mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 10));
  62. var_dump("MYSQLI_OPT_LOCAL_INFILE", mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, 1));
  63. var_dump("MYSQLI_INIT_COMMAND", mysqli_options($link, MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT=0'));
  64. /* mysqli_real_connect() */
  65. var_dump("MYSQLI_CLIENT_SSL", mysqli_options($link, MYSQLI_CLIENT_SSL, 'not a mysqli_option'));
  66. mysqli_close($link);
  67. echo "Link closed\n";
  68. try {
  69. mysqli_options($link, MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT=1');
  70. } catch (Error $exception) {
  71. echo $exception->getMessage() . "\n";
  72. }
  73. mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
  74. $link = mysqli_init();
  75. // test for error reporting - only mysqlnd reports errors
  76. try {
  77. mysqli_options($link, MYSQLI_SET_CHARSET_NAME, "foobar");
  78. if (!$IS_MYSQLND) {
  79. print "Unknown character set\n";
  80. }
  81. } catch (mysqli_sql_exception $e) {
  82. echo $e->getMessage() . "\n";
  83. }
  84. // invalid options do not generate errors
  85. mysqli_options($link, -1, "Invalid option");
  86. print "done!";
  87. ?>
  88. --EXPECTF--
  89. %s(25) "MYSQLI_READ_DEFAULT_GROUP"
  90. bool(true)
  91. %s(24) "MYSQLI_READ_DEFAULT_FILE"
  92. bool(true)
  93. %s(26) "MYSQLI_OPT_CONNECT_TIMEOUT"
  94. bool(true)
  95. %s(23) "MYSQLI_OPT_LOCAL_INFILE"
  96. bool(true)
  97. Warning: Array to string conversion in %s on line %d
  98. %s(19) "MYSQLI_INIT_COMMAND"
  99. bool(true)
  100. %s(25) "MYSQLI_READ_DEFAULT_GROUP"
  101. bool(true)
  102. %s(24) "MYSQLI_READ_DEFAULT_FILE"
  103. bool(true)
  104. %s(26) "MYSQLI_OPT_CONNECT_TIMEOUT"
  105. bool(true)
  106. %s(23) "MYSQLI_OPT_LOCAL_INFILE"
  107. bool(true)
  108. %s(19) "MYSQLI_INIT_COMMAND"
  109. bool(true)
  110. %s(17) "MYSQLI_CLIENT_SSL"
  111. bool(false)
  112. Link closed
  113. mysqli object is already closed
  114. Unknown character set
  115. done!