Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions ext/mysqli/mysqli_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1771,11 +1771,15 @@ PHP_FUNCTION(mysqli_options)
}
switch (expected_type) {
case IS_STRING:
ret = mysql_options(mysql->mysql, mysql_option, Z_STRVAL_P(mysql_value));
if ((ret = mysql_options(mysql->mysql, mysql_option, Z_STRVAL_P(mysql_value)))) {
MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
}
break;
case IS_LONG:
l_value = Z_LVAL_P(mysql_value);
ret = mysql_options(mysql->mysql, mysql_option, (char *)&l_value);
if ((ret = mysql_options(mysql->mysql, mysql_option, (char *)&l_value))) {
MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
}
break;
default:
ret = 1;
Expand Down
192 changes: 108 additions & 84 deletions ext/mysqli/tests/mysqli_options.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,99 +2,122 @@
mysqli_options()
--SKIPIF--
<?php
require_once('skipif.inc');
require_once('skipifconnectfailure.inc');
require_once 'skipif.inc';
require_once 'skipifconnectfailure.inc';

?>
--FILE--
<?php
require_once("connect.inc");

$valid_options = array(
MYSQLI_READ_DEFAULT_GROUP => "MYSQLI_READ_DEFAULT_GROUP",
MYSQLI_READ_DEFAULT_FILE => "MYSQLI_READ_DEFAULT_FILE",
MYSQLI_OPT_CONNECT_TIMEOUT => "MYSQLI_OPT_CONNECT_TIMEOUT",
MYSQLI_OPT_LOCAL_INFILE => "MYSQLI_OPT_LOCAL_INFILE",
MYSQLI_INIT_COMMAND => "MYSQLI_INIT_COMMAND",
MYSQLI_SET_CHARSET_NAME => "MYSQLI_SET_CHARSET_NAME",
MYSQLI_OPT_SSL_VERIFY_SERVER_CERT => "MYSQLI_OPT_SSL_VERIFY_SERVER_CERT",
);

if ($IS_MYSQLND && defined('MYSQLI_OPT_NET_CMD_BUFFER_SIZE'))
$valid_options[] = constant('MYSQLI_OPT_NET_CMD_BUFFER_SIZE');
if ($IS_MYSQLND && defined('MYSQLI_OPT_NET_READ_BUFFER_SIZE'))
$valid_options[] = constant('MYSQLI_OPT_NET_READ_BUFFER_SIZE');
if ($IS_MYSQLND && defined('MYSQLI_OPT_INT_AND_FLOAT_NATIVE'))
$valid_options[] = constant('MYSQLI_OPT_INT_AND_FLOAT_NATIVE');

$link = mysqli_init();

/* set it twice, checking if memory for the previous one is correctly freed */
mysqli_options($link, MYSQLI_SET_CHARSET_NAME, "utf8");
mysqli_options($link, MYSQLI_SET_CHARSET_NAME, "latin1");

// print "run_tests.php don't fool me with your 'ungreedy' expression '.+?'!\n";
var_dump("MYSQLI_READ_DEFAULT_GROUP", mysqli_options($link, MYSQLI_READ_DEFAULT_GROUP, 'extra_my.cnf'));
var_dump("MYSQLI_READ_DEFAULT_FILE", mysqli_options($link, MYSQLI_READ_DEFAULT_FILE, 'extra_my.cnf'));
var_dump("MYSQLI_OPT_CONNECT_TIMEOUT", mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 10));
var_dump("MYSQLI_OPT_LOCAL_INFILE", mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, 1));
var_dump("MYSQLI_INIT_COMMAND", mysqli_options($link, MYSQLI_INIT_COMMAND, array('SET AUTOCOMMIT=0', 'SET AUTOCOMMIT=1')));

if (!$link2 = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
printf("[006] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
$host, $user, $db, $port, $socket);

if (!$res = mysqli_query($link2, 'SELECT version() AS server_version'))
printf("[007] [%d] %s\n", mysqli_errno($link2), mysqli_error($link2));
$tmp = mysqli_fetch_assoc($res);
mysqli_free_result($res);
$version = explode('.', $tmp['server_version']);
if (empty($version))
printf("[008] Cannot determine server version, need MySQL Server 4.1+ for the test!\n");

if ($version[0] <= 4 && $version[1] < 1)
printf("[009] Need MySQL Server 4.1+ for the test!\n");

if (!$res = mysqli_query($link2, "SHOW CHARACTER SET"))
printf("[010] Cannot get list of available character sets, [%d] %s\n",
mysqli_errno($link2), mysqli_error($link2));

$charsets = array();
while ($row = mysqli_fetch_assoc($res))
$charsets[] = $row;
mysqli_free_result($res);
mysqli_close($link2);

foreach ($charsets as $charset) {
$k = $charset['Charset'];
/* The server currently 17.07.2007 can't handle data sent in ucs2 */
/* The server currently 16.08.2010 can't handle data sent in utf16 and utf32 */
if ($charset['Charset'] == 'ucs2' || $charset['Charset'] == 'utf16' || $charset['Charset'] == 'utf32') {
continue;
}
if (true !== mysqli_options($link, MYSQLI_SET_CHARSET_NAME, $charset['Charset'])) {
printf("[009] Setting charset name '%s' has failed\n", $charset['Charset']);
}
require_once "connect.inc";

$valid_options = array(
MYSQLI_READ_DEFAULT_GROUP => "MYSQLI_READ_DEFAULT_GROUP",
MYSQLI_READ_DEFAULT_FILE => "MYSQLI_READ_DEFAULT_FILE",
MYSQLI_OPT_CONNECT_TIMEOUT => "MYSQLI_OPT_CONNECT_TIMEOUT",
MYSQLI_OPT_LOCAL_INFILE => "MYSQLI_OPT_LOCAL_INFILE",
MYSQLI_INIT_COMMAND => "MYSQLI_INIT_COMMAND",
MYSQLI_SET_CHARSET_NAME => "MYSQLI_SET_CHARSET_NAME",
MYSQLI_OPT_SSL_VERIFY_SERVER_CERT => "MYSQLI_OPT_SSL_VERIFY_SERVER_CERT",
);

if ($IS_MYSQLND && defined('MYSQLI_OPT_NET_CMD_BUFFER_SIZE')) {
$valid_options[] = constant('MYSQLI_OPT_NET_CMD_BUFFER_SIZE');
}
if ($IS_MYSQLND && defined('MYSQLI_OPT_NET_READ_BUFFER_SIZE')) {
$valid_options[] = constant('MYSQLI_OPT_NET_READ_BUFFER_SIZE');
}
if ($IS_MYSQLND && defined('MYSQLI_OPT_INT_AND_FLOAT_NATIVE')) {
$valid_options[] = constant('MYSQLI_OPT_INT_AND_FLOAT_NATIVE');
}

$link = mysqli_init();

/* set it twice, checking if memory for the previous one is correctly freed */
mysqli_options($link, MYSQLI_SET_CHARSET_NAME, "utf8");
mysqli_options($link, MYSQLI_SET_CHARSET_NAME, "latin1");

// print "run_tests.php don't fool me with your 'ungreedy' expression '.+?'!\n";
var_dump("MYSQLI_READ_DEFAULT_GROUP", mysqli_options($link, MYSQLI_READ_DEFAULT_GROUP, 'extra_my.cnf'));
var_dump("MYSQLI_READ_DEFAULT_FILE", mysqli_options($link, MYSQLI_READ_DEFAULT_FILE, 'extra_my.cnf'));
var_dump("MYSQLI_OPT_CONNECT_TIMEOUT", mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 10));
var_dump("MYSQLI_OPT_LOCAL_INFILE", mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, 1));
var_dump("MYSQLI_INIT_COMMAND", mysqli_options($link, MYSQLI_INIT_COMMAND, array('SET AUTOCOMMIT=0', 'SET AUTOCOMMIT=1')));

if (!$link2 = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
printf("[006] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
$host, $user, $db, $port, $socket);
}

if (!$res = mysqli_query($link2, 'SELECT version() AS server_version')) {
printf("[007] [%d] %s\n", mysqli_errno($link2), mysqli_error($link2));
}
$tmp = mysqli_fetch_assoc($res);
mysqli_free_result($res);
$version = explode('.', $tmp['server_version']);
if (empty($version)) {
printf("[008] Cannot determine server version, need MySQL Server 4.1+ for the test!\n");
}

if ($version[0] <= 4 && $version[1] < 1) {
printf("[009] Need MySQL Server 4.1+ for the test!\n");
}

if (!$res = mysqli_query($link2, "SHOW CHARACTER SET")) {
printf("[010] Cannot get list of available character sets, [%d] %s\n",
mysqli_errno($link2), mysqli_error($link2));
}

$charsets = array();
while ($row = mysqli_fetch_assoc($res)) {
$charsets[] = $row;
}
mysqli_free_result($res);
mysqli_close($link2);

foreach ($charsets as $charset) {
$k = $charset['Charset'];
/* The server currently 17.07.2007 can't handle data sent in ucs2 */
/* The server currently 16.08.2010 can't handle data sent in utf16 and utf32 */
if ($charset['Charset'] == 'ucs2' || $charset['Charset'] == 'utf16' || $charset['Charset'] == 'utf32') {
continue;
}
if (true !== mysqli_options($link, MYSQLI_SET_CHARSET_NAME, $charset['Charset'])) {
printf("[009] Setting charset name '%s' has failed\n", $charset['Charset']);
}
}

var_dump("MYSQLI_READ_DEFAULT_GROUP", mysqli_options($link, MYSQLI_READ_DEFAULT_GROUP, 'extra_my.cnf'));
var_dump("MYSQLI_READ_DEFAULT_FILE", mysqli_options($link, MYSQLI_READ_DEFAULT_FILE, 'extra_my.cnf'));
var_dump("MYSQLI_OPT_CONNECT_TIMEOUT", mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 10));
var_dump("MYSQLI_OPT_LOCAL_INFILE", mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, 1));
var_dump("MYSQLI_INIT_COMMAND", mysqli_options($link, MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT=0'));
var_dump("MYSQLI_READ_DEFAULT_GROUP", mysqli_options($link, MYSQLI_READ_DEFAULT_GROUP, 'extra_my.cnf'));
var_dump("MYSQLI_READ_DEFAULT_FILE", mysqli_options($link, MYSQLI_READ_DEFAULT_FILE, 'extra_my.cnf'));
var_dump("MYSQLI_OPT_CONNECT_TIMEOUT", mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 10));
var_dump("MYSQLI_OPT_LOCAL_INFILE", mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, 1));
var_dump("MYSQLI_INIT_COMMAND", mysqli_options($link, MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT=0'));

/* mysqli_real_connect() */
var_dump("MYSQLI_CLIENT_SSL", mysqli_options($link, MYSQLI_CLIENT_SSL, 'not a mysqli_option'));
/* mysqli_real_connect() */
var_dump("MYSQLI_CLIENT_SSL", mysqli_options($link, MYSQLI_CLIENT_SSL, 'not a mysqli_option'));

mysqli_close($link);
mysqli_close($link);

echo "Link closed\n";
try {
mysqli_options($link, MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT=1');
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}
echo "Link closed\n";
try {
mysqli_options($link, MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT=1');
} catch (Error $exception) {
echo $exception->getMessage() . "\n";
}

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = mysqli_init();

// test for error reporting
try {
mysqli_options($link, MYSQLI_SET_CHARSET_NAME, "foobar");
} catch (mysqli_sql_exception $e) {
echo $e->getMessage() . "\n";
}

// invalid options do not generate errors
mysqli_options($link, -1, "Invalid option");

print "done!";
print "done!";
?>
--EXPECTF--
%s(25) "MYSQLI_READ_DEFAULT_GROUP"
Expand Down Expand Up @@ -123,4 +146,5 @@ bool(true)
bool(false)
Link closed
mysqli object is already closed
Unknown character set
done!