mysqli_options.phpt 5.4 KB

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