[5/5] Fix SQL query to retrieve language setting

Message ID 20170227184955.25495-5-lfleischer@archlinux.org
State Accepted, archived
Headers show
Series [1/5] 404.php: Squelch warning on empty PATH_INFO | expand

Commit Message

Lukas Fleischer Feb. 27, 2017, 6:49 p.m. UTC
In commit e171f6f (Migrate all DB code to use PDO, 2012-08-08),
PDOStatement::fetchAll() was introduced as a drop-in replacement for
mysql_fetch_array(). However, PDOStatement::fetchAll() returns a list of
all results while mysql_fetch_array() returns a single result only.
Instead of adding the missing indirection, simplify the code by using
PDO::fetchColumn().

Also add some safeguards to prevent warnings if the result set returned
by the query is empty.

Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
---
 web/lib/translator.inc.php | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

Patch

diff --git a/web/lib/translator.inc.php b/web/lib/translator.inc.php
index 58648c4..d10f8e9 100644
--- a/web/lib/translator.inc.php
+++ b/web/lib/translator.inc.php
@@ -111,14 +111,16 @@  function set_lang() {
 		$result = $dbh->query($q);
 
 		if ($result) {
-			$row = $result->fetchAll();
-			$LANG = $row[0];
+			$LANG = $result->fetchColumn(0);
+			if (!$LANG) {
+				unset($LANG);
+			}
 		}
 		$update_cookie = 1;
 	}
 
 	# Set $LANG to default if nothing is valid.
-	if (!array_key_exists($LANG, $SUPPORTED_LANGS)) {
+	if (!isset($LANG) || !array_key_exists($LANG, $SUPPORTED_LANGS)) {
 		$LANG = config_get('options', 'default_lang');
 	}