admin/stories.php, includes/mysqli_functions.php: Database function changes from eFiction 3.5.72 - keep the MySQL strict mode behavior
admin/stories.php, includes/mysqli_functions.php: Database function changes from eFiction 3.5.72 - keep the MySQL strict mode behavior

--- a/admin/stories.php
+++ b/admin/stories.php
@@ -32,7 +32,7 @@
 	if($_GET['feature'])
 		dbquery("UPDATE ".TABLEPREFIX."fanfiction_stories SET featured = 1 WHERE sid = ".$_GET['feature']);
 	$fquery = "SELECT stories.*, stories.title as title, author.penname, DATE_FORMAT(FROM_UNIXTIME(stories.updated), '%Y.%m.%d') as updatesort, DATE_FORMAT(stories.date, '$datim') as date, DATE_FORMAT(FROM_UNIXTIME(stories.updated), '$datim') as updated FROM ".TABLEPREFIX."fanfiction_authors as author, ".TABLEPREFIX."fanfiction_stories as stories WHERE stories.featured > 0 AND stories.uid = author.uid ORDER BY stories.featured";
-	$fresult = dbquery($fquery) or die(_FATALERROR."Query: ".$fquery."<br />Error: (". $dbconnect->errno.") ". $dbconnect->error);
+	$fresult = dbquery($fquery) or die(_FATALERROR."Query: ".$fquery."<br />Error: ".dberror());
 	$output = "<center><table class=\"tblborder\" cellpadding=\"5\"><tr><th colspan=\"2\" align=\"center\">"._FEATUREDSTORIES."</th></tr>";
 	if(!dbnumrows($fresult)) $output .= "<tr><td colspan=\"2\" align=\"center\">"._NOSTORIES."</td><tr>";
 	while($story = dbassoc($fresult)) {
@@ -48,7 +48,7 @@
 	global $tableprefix, $admincats, $allowed_tags, $dbconnect;
 		$output = "<center><h4>"._SUBMITTED."</h4></center>";
 		$query = "SELECT story.title as storytitle, chapter.uid, chapter.sid, story.catid, chapter.chapid, chapter.inorder, chapter.title, author.penname FROM (".TABLEPREFIX."fanfiction_chapters as chapter, ".TABLEPREFIX."fanfiction_authors as author) LEFT JOIN ".TABLEPREFIX."fanfiction_stories as story ON story.sid = chapter.sid WHERE chapter.validated = '0' AND chapter.uid = author.uid ORDER BY story.title ASC, chapter.inorder ASC";
-		$result = dbquery($query) or die(_FATALERROR."Query: ".$query."<br />Error: (". $dbconnect->errno.") ". $dbconnect->error);
+		$result = dbquery($query) or die(_FATALERROR."Query: ".$query."<br />Error: ".dberror());
 		$output .= "<table class=\"tblborder\" cellspacing=\"0\" cellpadding=\"3\" align=\"center\"><tr class=\"tblborder\"><th>"._TITLE."</th><th>"._AUTHOR."</th><th>"._CATEGORY."</th><th>"._OPTIONS."</th></tr>";
 		$array = explode(",", $admincats);
 		while ($story = dbassoc($result))
@@ -239,3 +239,4 @@
 // end noletter
 
 ?>
+

--- a/includes/mysqli_functions.php
+++ b/includes/mysqli_functions.php
@@ -35,6 +35,12 @@
 	return $mysqli_access;
 }
 
+// Error text of the last statement, for diagnostics in die() strings: "(errno) message".
+function dberror() {
+	global $dbconnect;
+	if(!($dbconnect instanceof mysqli)) return "";
+	return "(".$dbconnect->errno.") ".$dbconnect->error;
+}
 
 function dbquery($query) {
 	global $debug, $headerSent, $dbconnect;
@@ -47,32 +53,40 @@
 	catch (mysqli_sql_exception $e)
 	{
 		if(function_exists('accessDenied')) {
-			accessDenied(_FATALERROR . (isADMIN ? "Query: " . $query . "<br />Error: (" . $dbconnect->errno . ") " . $dbconnect->error : ""));
+			accessDenied(_FATALERROR . (isADMIN ? "Query: " . $query . "<br />Error: " . dberror() : ""));
 		}
-		else {		
+		else {
 			// this is very rare case. Uncomment this if you really lost what is doing. This is before pagesetup.php, so Admin is not set
-			//	echo "Query: " . $query . "<br />Error: (" . $dbconnect->errno . ") " . $dbconnect->error  <br /> ;
-			die(" <b>A fatal MySQL error was encountered.</b><br /> Check tableprefix. ");	
+			//	echo "Query: " . $query . "<br />Error: " . dberror() "<br />" ;
+			die(" <b>A fatal MySQL error was encountered.</b><br /> Check tableprefix. ");
 		}
-			
-	} 
+
+	}
 	return $result;
 }
 
 function dbnumrows($query) {
 	global $debug, $dbconnect;
 	if ($query === false && $debug) {
-		echo "<!-- dbnumrows ".$dbconnect->error." -->\n";
+		echo "<!-- dbnumrows ".($dbconnect ? $dbconnect->error : "")." -->\n";
 	}
-	return $query->num_rows;
+	return $query === false ? 0 : $query->num_rows;
+}
+
+function dbfetcharray($query) {
+	global $debug, $dbconnect;
+	if ($query === false && $debug) {
+		echo "<!-- dbfetcharray ".($dbconnect ? $dbconnect->error : "")." -->\n";
+	}
+	return $query === false ? null : $query->fetch_array();
 }
 
 function dbassoc($query) {
 	global $debug, $dbconnect;
 	if ($query === false && $debug) {
-		echo "<!-- dbassoc ".$dbconnect->errno." -->\n";
+		echo "<!-- dbassoc ".($dbconnect ? $dbconnect->error : "")." -->\n";
 	}
-	return $query->fetch_assoc();
+	return $query === false ? null : $query->fetch_assoc();
 }
 
 function dbinsertid($tablename = 0) {
@@ -83,14 +97,14 @@
 function dbrow($query) {
 	global $debug, $dbconnect;
 	if ($query === false && $debug) {
-		if($dbconnect->error) echo "<!-- dbrow ".$dbconnect->errno." -->\n";
+		if($dbconnect->error) echo "<!-- dbrow ".($dbconnect ? $dbconnect->error : "")." -->\n";
 	}
-	return $query->fetch_row();
+	return $query === false ? null : $query->fetch_row();
 }
 
 function dbclose() {
 	global $dbconnect;
-	$dbconnect->close();
+	if($dbconnect instanceof mysqli) $dbconnect->close();
 }
 
 // Used to escape text being put into the database.