From a8fdfc35bd1fbadacfc910382dddedd6c9a37679 Mon Sep 17 00:00:00 2001 From: garvinhicking Date: Fri, 30 Mar 2007 10:02:05 +0000 Subject: [PATCH] Allow non-standard port specification --- docs/NEWS | 3 +++ include/db/mysqli.inc.php | 45 ++++++++++++++++++++++++++++++++------- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/docs/NEWS b/docs/NEWS index 2987584..da8f7b3 100644 --- a/docs/NEWS +++ b/docs/NEWS @@ -3,6 +3,9 @@ Version 1.2 () ------------------------------------------------------------------------ + * Allow to specify non-default port when using MySQLi + (garvinhicking) + * Show current captcha look in the plugin configuration menu (garvinhicking) diff --git a/include/db/mysqli.inc.php b/include/db/mysqli.inc.php index 653ed63..c52f2ea 100644 --- a/include/db/mysqli.inc.php +++ b/include/db/mysqli.inc.php @@ -107,9 +107,13 @@ function &serendipity_db_query($sql, $single = false, $result_type = "both", $re $rows = array(); while ($row = mysqli_fetch_array($c, $result_type)) { - if (!empty($assocKey) && !empty($assocVal)) { + if (!empty($assocKey)) { // You can fetch a key-associated array via the two function parameters assocKey and assocVal - $rows[$row[$assocKey]] = $row[$assocVal]; + if (empty($assocVal)) { + $rows[$row[$assocKey]] = $row; + } else { + $rows[$row[$assocKey]] = $row[$assocVal]; + } } else { $rows[] = $row; } @@ -226,16 +230,31 @@ function serendipity_db_connect() { $function = 'mysqli_connect'; - $serendipity['dbConn'] = $function($serendipity['dbHost'], $serendipity['dbUser'], $serendipity['dbPass']); - mysqli_select_db($serendipity['dbConn'], $serendipity['dbName']); - - if (defined('SQL_CHARSET') && $serendipity['dbNames']) { - mysqli_query($serendipity['dbConn'], "SET NAMES " . SQL_CHARSET); + $connparts = explode(':', $serendipity['dbHost']); + if (!empty($connparts[1])) { + // A "hostname:port" connection was specified + $serendipity['dbConn'] = $function($connparts[0], $serendipity['dbUser'], $serendipity['dbPass'], $serendipity['dbName'], $connparts[1]); + } else { + // Connect with default ports + $serendipity['dbConn'] = $function($connparts[0], $serendipity['dbUser'], $serendipity['dbPass']); } + mysqli_select_db($serendipity['dbConn'], $serendipity['dbName']); + serendipity_db_reconnect(); return $serendipity['dbConn']; } +function serendipity_db_reconnect() { + global $serendipity; + + if (isset($serendipity['dbCharset'])) { + mysqli_query($serendipity['dbConn'], "SET NAMES " . $serendipity['dbCharset']); + define('SQL_CHARSET_INIT', true); + } elseif (defined('SQL_CHARSET') && $serendipity['dbNames'] && !defined('SQL_CHARSET_INIT')) { + mysqli_query($serendipity['dbConn'], "SET NAMES " . SQL_CHARSET, $serendipity['dbConn']); + } +} + /** * Prepares a Serendipty query input to fully valid SQL. Replaces certain "template" variables. * @@ -288,7 +307,17 @@ function serendipity_db_probe($hash, &$errs) { return false; } - if (!($c = @mysqli_connect($hash['dbHost'], $hash['dbUser'], $hash['dbPass']))) { + $function = 'mysqli_connect'; + $connparts = explode(':', $hash['dbHost']); + if (!empty($connparts[1])) { + // A "hostname:port" connection was specified + $c = @$function($connparts[0], $hash['dbUser'], $hash['dbPass'], $hash['dbName'], $connparts[1]); + } else { + // Connect with default ports + $c = @$function($connparts[0], $hash['dbUser'], $hash['dbPass']); + } + + if (!$c) { $errs[] = 'Could not connect to database; check your settings.'; $errs[] = 'The mySQL error was: ' . mysqli_connect_error(); return false; -- 2.39.5