MyBB Community Forums

Full Version: Query returns unexpected results
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am using the following code in a custom page/template where I want to display two specific fields from the posts table. The result of $peekmessage is NULL. When I reverse the order of variable assignment statements lines 3 and 4, the result of $peeksubject is NULL. Apparently I can only fetch the content of one field. Obviously I'm doing something wrong.

Why do two sequential fetch_fields fail?

$peeksubject = ''; $peekmessage = '';
$query = $db->query("SELECT * FROM " . TABLE_PREFIX . "posts WHERE pid = 637219");
$peeksubject = $db->fetch_field($query, "subject");
$peekmessage = $db->fetch_field($query, "message");

echo("<pre>");
var_dump($db);
var_dump($query);
var_dump($peeksubject);
var_dump($peekmessage);
echo("</pre>");
Why you don't use this:
global $db;
$query = $db->simple_select("posts","subject, message","pid = '637219'");
$result = $db->fetch_array($query);
if($result['subject'])
{
	//do this;
}

if($result['message'])
{
	//do this;
}
Maybe MySQL/PHP has internally limited such manipulations. Use or die(mysqli_error()) (for mysqli, or or die(mysql_error()) for mysql) to print error messages from MySQL for more details. (see https://stackoverflow.com/a/12227652/6681141)

Anyway, I think you can just obtain those fields using following code:
$result = $db->fetch_array($query);
$peeksubject = $result['subject'];
$peekmessage =  $result['message'];
I marked Sven's answer as best because he gave me (a noob Big Grin) the clue for a simpler query method.
However I also liked noyle's answer for direct assignment to variables.

Rep+ to both for helping. Thanks to both.

This little project is part of a test to convert mb4 text fields that are coded one way but represented differently. Following this method for custom forum page. https://community.mybb.com/thread-116225.html

PHP --
<?php 

define('IN_MYBB', 1); require "./global.php";

add_breadcrumb("Language peek", "peek.php"); 

$query = $db->simple_select("posts","subject, message","pid = '637219'");
$result = $db->fetch_array($query);
	$peeksubject = $result['subject'];	//do this;
	$peekmessage = $result['message'];	//do this;

// echo("<pre>");
// var_dump($db); echo "\n";
// var_dump($query); echo "\n";
// var_dump($peeksubject); echo "\n";
// var_dump($peekmessage); echo "\n";
// echo("</pre>");

eval("\$html = \"".$templates->get("languagepeek")."\";"); 
output_page($html);

?>

Template code --
<html>
<head>
<title>Language peek</title>
{$headerinclude}
</head>
<body>
{$header}
<table border="0" cellspacing="1" cellpadding="4" class="tborder">
<tr>
<td class="thead"><span class="smalltext"><strong>Peeking into messages for language display.</strong></span></td>
</tr>
<tr>
<td class="trow1">

$peeksubject

</td><td>

$peekmessage

</td></tr></table>
{$footer}
</body>
</html>
You don't need the extra variables. Wink

Try this:

PHP:
<?php 

define('IN_MYBB', 1); require "./global.php";

add_breadcrumb("Language peek", "peek.php"); 

$query = $db->simple_select("posts","subject, message","pid = '637219'");
$result = $db->fetch_array($query);

$result['subject'] = htmlspecialchars_uni($result['subject']);
$result['message'] = htmlspecialchars_uni($result['message']);

// echo("<pre>");
// var_dump($db); echo "\n";
// var_dump($query); echo "\n";
// var_dump($peeksubject); echo "\n";
// var_dump($peekmessage); echo "\n";
// echo("</pre>");

eval("\$html = \"".$templates->get("languagepeek")."\";"); 
output_page($html);


Template:
<html>
<head>
<title>Language peek</title>
{$headerinclude}
</head>
<body>
{$header}
<table width="100%" border="0" cellspacing="{$theme['borderwidth']}" cellpadding="{$theme['tablespace']}" class="tborder">
<tr>
 <td class="thead" colspan="2"><strong>Peeking into messages for language display.</strong></td>
</tr>
<tr>
 <td class="trow1">{$result['subject']}</td>
 <td class="trow1">{$result['message']}</td>
</tr>
</table>
{$footer}
</body>
</html>
I'm working to evaluate a local converion function that I found, so want to compare the before and after.

function parseUtf8ToIso88591(&$string){
     if(!is_null($string)){
            $iso88591_1 = utf8_decode($string);
            $iso88591_2 = iconv('UTF-8', 'ISO-8859-1', $string);
            $string = mb_convert_encoding($string, 'ISO-8859-1', 'UTF-8');       
     }
}