function test_fetch($host, $user, $passwd, $db, $port, $socket, $engine, $flags = null) { $link = mysqli_init(); if (!my_mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket, $flags)) { printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n", $host, $user, $db, $port, $socket); return false; } if (!mysqli_query($link, "DROP TABLE IF EXISTS test") || !mysqli_query($link, sprintf("CREATE TABLE test(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, label VARCHAR(255)) ENGINE = %s", $engine))) { printf("[002] [%d] %s\n", mysqli_errno($link), mysqli_error($link)); return false; } $package_size = 524288; $offset = 3; $limit = ini_get('memory_limit') > 0 ? parse_memory_limit(ini_get('memory_limit')) : pow(2, 32); /* try to respect php.ini but make run time a soft limit */ $max_runtime = ini_get('max_execution_time') > 0 ? ini_get('max_execution_time') : 30; set_time_limit(0); do { if ($package_size > $limit) { printf("stop: memory limit - %s vs. %s\n", $package_size, $limit); break; } $start = microtime(true); if (!mysqli_fetch_array_large($offset++, $link, $package_size)) { printf("stop: packet size - %d\n", $package_size); break; } $duration = microtime(true) - $start; $max_runtime -= $duration; if ($max_runtime < $duration * 3) { /* likely the next iteration will not be within max_execution_time */ printf("stop: time limit - %2.2fs\n", $max_runtime); break; } $package_size += $package_size; } while (true); mysqli_close($link); return true; }
if (!($res = mysqli_query($link, "SELECT id FROM test"))) { printf("[006] [%d] %s\n", mysqli_errno($link), mysqli_error($link)); continue; } $row = mysqli_fetch_assoc($res); mysqli_free_result($res); if ($row['id'] !== $data[1]) { printf("[007] Expecting %s - %s/%s got %s/%s\n", $name, $data[1], gettype($data[1]), $row['id'], gettype($row['id'])); } mysqli_close($link); $link = mysqli_init(); if (!mysqli_options($link, MYSQLI_OPT_INT_AND_FLOAT_NATIVE, 0)) { printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link)); continue; } if (!my_mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket)) { printf("[009] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()); continue; } if (!($res = mysqli_query($link, "SELECT id FROM test"))) { printf("[010] [%d] %s\n", mysqli_errno($link), mysqli_error($link)); continue; } $row = mysqli_fetch_assoc($res); mysqli_free_result($res); if (!is_string($row['id']) || $row['id'] != $data[1]) { printf("[011] Expecting %s - %s/string got %s/%s\n", $name, $data[1], $row['id'], gettype($row['id'])); } mysqli_close($link); } print "done!";
<?php require_once "connect.inc"; $host = 'p:' . $host; if (!($link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))) { printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n", $host, $user, $db, $port, $socket); } if (true !== ($tmp = my_mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket))) { printf("[003] Expecting boolean/true got %s/%s\n", gettype($tmp), $tmp); } /* it is undefined which pooled connection we get - thread ids may differ */ if (!($res = mysqli_query($link, "SELECT 'ok' AS it_works")) || !($row = mysqli_fetch_assoc($res))) { printf("[006] [%d] %s\n", mysqli_errno($link), mysqli_error($link)); } var_dump($row); mysqli_free_result($res); mysqli_close($link); if (!($link = new my_mysqli($host, $user, $passwd, $db, $port, $socket))) { printf("[007] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n", $host, $user, $db, $port, $socket); } if (true !== ($tmp = $link->real_connect($host, $user, $passwd, $db, $port, $socket))) { printf("[009] Expecting boolean/true got %s/%s\n", gettype($tmp), $tmp); } /* it is undefined which pooled connection we get - thread ids may differ */ if (!($res = $link->query("SELECT 'works also with oo' AS syntax")) || !($row = $res->fetch_assoc())) { printf("[012] [%d] %s\n", mysqli_errno($link), mysqli_error($link)); } var_dump($row); mysqli_free_result($res); mysqli_close($link); if (NULL !== ($tmp = $link->connect($host, $user, $passwd, $db, $port, $socket))) {
printf("[004] Connect should fail, [%d] %s\n", $link->errno, $link->error); } /* allow connect */ $link = mysqli_init(); $link->options(MYSQLI_OPT_CAN_HANDLE_EXPIRED_PASSWORDS, 1); if (!my_mysqli_real_connect($link, $host, 'expiretest', "", $db, $port, $socket)) { printf("[005] Cannot connect [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()); } else { $link->query("SELECT id FROM test WHERE id = 1"); printf("[006] Connect allowed, query fail, [%d] %s\n", $link->errno, $link->error); $link->close(); } /* allow connect, fix pw */ $link = mysqli_init(); $link->options(MYSQLI_OPT_CAN_HANDLE_EXPIRED_PASSWORDS, 1); if (!my_mysqli_real_connect($link, $host, 'expiretest', "", $db, $port, $socket)) { printf("[007] Cannot connect [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()); } else { $link->query("SET PASSWORD=PASSWORD('expiretest')"); printf("[008] Connect allowed, pw set, [%d] %s\n", $link->errno, $link->error); if ($res = $link->query("SELECT id FROM test WHERE id = 1")) { var_dump($res->fetch_assoc()); } $link->close(); } /* check login */ if (!($link = my_mysqli_connect($host, 'expiretest', "expiretest", $db, $port, $socket))) { printf("[001] Cannot connect [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()); } else { $link->query("SELECT id FROM test WHERE id = 1"); if ($res = $link->query("SELECT id FROM test WHERE id = 1")) {
// Check that none of the above would have caused any error messages if MYSQL_REPORT_ERROR would // not have been set. If that would be the case, the test would be broken. mysqli_report(MYSQLI_REPORT_OFF); mysqli_multi_query($link, "BAR; FOO;"); mysqli_query($link, "FOO"); mysqli_change_user($link, "This might work if you accept anonymous users in your setup", "password", $db); mysqli_kill($link, -1); mysqli_real_query($link, "FOO"); mysqli_select_db($link, "Oh lord, let this be an unknown database name"); mysqli_report(MYSQLI_REPORT_OFF); mysqli_report(MYSQLI_REPORT_STRICT); try { if ($link = my_mysqli_connect($host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket)) { printf("[010] Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n", $host, $user . 'unknown_really', $db, $port, $socket); } mysqli_close($link); } catch (mysqli_sql_exception $e) { printf("[011] %s\n", $e->getMessage()); } try { if (!($link = mysqli_init())) { printf("[012] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()); } if ($link = my_mysqli_real_connect($link, $host, $user . 'unknown_really', $passwd . 'non_empty', $db, $port, $socket)) { printf("[013] Can connect to the server using host=%s, user=%s, passwd=***non_empty, dbname=%s, port=%s, socket=%s\n", $host, $user . 'unknown_really', $db, $port, $socket); } mysqli_close($link); } catch (mysqli_sql_exception $e) { printf("[014] %s\n", $e->getMessage()); } print "done!";
<?php include "connect.inc"; $db1 = new mysqli(); $flags = MYSQLI_CLIENT_SSL; $link = mysqli_init(); mysqli_ssl_set($link, null, null, null, null, "RC4-MD5"); if (my_mysqli_real_connect($link, 'p:' . $host, $user, $passwd, $db, $port, null, $flags)) { $r = $link->query("SHOW STATUS LIKE 'Ssl_cipher'"); var_dump($r->fetch_row()); } /* non-persistent connection */ $link2 = mysqli_init(); mysqli_ssl_set($link2, null, null, null, null, "RC4-MD5"); if (my_mysqli_real_connect($link2, $host, $user, $passwd, $db, $port, null, $flags)) { $r2 = $link2->query("SHOW STATUS LIKE 'Ssl_cipher'"); var_dump($r2->fetch_row()); } echo "done\n";
<?php include "connect.inc"; $db1 = new mysqli(); // These calls fail $db1->options(MYSQLI_OPT_CONNECT_TIMEOUT, 3); my_mysqli_real_connect($db1, $host, $user, $passwd, $db, $port, $socket); if (mysqli_connect_error()) { echo "error 1\n"; } else { echo "ok 1\n"; } $db2 = mysqli_init(); $db2->options(MYSQLI_OPT_CONNECT_TIMEOUT, 3); my_mysqli_real_connect($db2, $host, $user, $passwd, $db, $port, $socket); if (mysqli_connect_error()) { echo "error 2\n"; } else { echo "ok 2\n"; } echo "done\n";