hello Everyone

I was wondering if anyone can help me with a little php and mysql problem occurred to the CMS i am using.

I have a function which is get data from a table after updating the table, the code is as follow:

public function getHtmlWrapper($id) {
	 	//get entire html wrapper
		$sql = "SELECT id, theme_name, html_wrapper FROM ".TABLE_PREFIX."themes WHERE id = $id";
        $result_id = $this->db->query($sql);        
        $theme_data = $this->db->fetchToRow($result_id);
        
        //read file in to database
        $file_content = file_get_contents(SITE_ROOT.'/themes/'.$theme_data["theme_name"].'/index.php');
        if($file_content) {
         $this->db->update('themes',
		 	array('html_wrapper'=>$file_content),
			 "id = '".(int) $id."'");
        }
        
        //get entire html wrapper
		$sql = "SELECT id, theme_name, html_wrapper FROM ".TABLE_PREFIX."themes WHERE id = $id";
        $result_id = $this->db->query($sql);        
        $theme_data = $this->db->fetchToRow($result_id);
        return $theme_data;
    }

the " $this->db->query " is calling a update function in the database class which is: as follow:

function update($table, $data, $where='') {

	    $q="UPDATE ".TABLE_PREFIX."".$table." SET ";
	
	    foreach($data as $key=>$val) {
	        if(strtolower($val)=='null') $q.= "`$key` = NULL, ";
	        elseif(strtolower($val)=='now()') $q.= "`$key` = NOW(), ";
	        else $q.= "`$key`='".$this->escape($val)."', ";
	    }
	
	    $q = rtrim($q, ', ') . ' WHERE '.$where.';';
		//echo $q;
	    return $this->query($q);
	}

function escape($string) {
		if(version_compare(phpversion(),"4.3.0")=="-1") {
			return mysql_escape_string($string);
		} else {
			return mysql_real_escape_string($string);
		}
	}

when I do var_dump($file_content) before update statement it gives me the contents :

<? include "layout/header.php" ?>
<? $body = (isset($body))?$body:'home'?>
<? include 'contents/'.$body.'.php' ?>
<? include 'layout/footer.php' ?>

but once the table is updated, the "html_wrapper" field in the table has an extra ? before:

<? include "layout/header.php" ?>
<? $body = (isset($body))?$body:'home'?>
<? include 'contents/'.$body.'.php' ?>
<? include 'layout/footer.php' ?>

that makes the "html_wrapper" field has the value:

?<? include "layout/header.php" ?>
<? $body = (isset($body))?$body:'home'?>
<? include 'contents/'.$body.'.php' ?>
<? include 'layout/footer.php' ?>

I was wondering where the extra ? at the begining come from?

very much appreciated for any help and suggestions?

thank you

Dani AI

Generated

Extra ? at the very start almost always comes from a stray byte or a charset/escaping mismatch — not from the PHP short tag itself. and were right to flag the visible symptom and to suggest inspecting the built query. Follow these focused checks in order to pinpoint and fix it.

Check the raw bytes first (quick, reliable test)

  • In MySQL you can inspect the first bytes stored:
    SELECT HEX(LEFT(html_wrapper,3)) FROM themes WHERE id = <your id>;

    If you see EFBBBF (UTF‑8 BOM) or unexpected hex values, that explains the extra character.

Detect and remove a UTF‑8 BOM in PHP

  • If the source file was saved with a BOM, remove it before update:
    if (substr($s, 0, 3) === "\xEF\xBB\xBF") {
        $s = substr($s, 3);
    }

Charset and escaping issues

  • mysql_real_escape_string must be used after a proper DB connection and with the correct client charset set. If the connection charset differs from the file encoding, bytes can be replaced or mangled. Call the client‑charset function (or use PDO/mysqli with charset set) right after connect; see the PHP docs for details: mysql_real_escape_string and mysql_set_charset.
  • Also confirm the column collation/charset (use utf8mb4 if needed).

Practical troubleshooting and fixes

  • Echo the final SQL query string before running it to look for stray characters (as suggested).
  • Try inserting a controlled test string to see if the update routine adds a character.
  • Save template files as "UTF‑8 without BOM" (most editors offer this).
  • Prefer prepared statements (PDO/mysqli) to avoid manual escaping and charset pitfalls.
  • Replace short tags with full <?php tags to avoid any parsing confusion.

If tests show no BOM or charset mismatch, report the HEX output and the echoed SQL; that will narrow the cause further.

Recommended Answers

All 8 Replies

Tried to read through it, didn't see anything-but could you implement the CODE tags? That way I can see what's code easier, and read what you're writing easier.

I really can't be bothered to read your code fully if you can't be bothered to use

tags. (It does have a 'watermark' in the post boxes which tell you about the code tags)

But a quick glance shows this on your last lines:
[code=php]?<? include "layout/header.php" ?>

Whats with the extra ? there.

Member Avatar for Member #120589

It looks like this line:

?<? include "layout/header.php" ?>

Take out the '?' before the the tagged php include statement.

The

?<? include "layout/header.php" ?>

is the output, apparently. I think he's wondering what's causing the initial ? before the include.

Member Avatar for Member #120589

The

?<? include "layout/header.php" ?>

is the output, apparently. I think he's wondering what's causing the initial ? before the include.

Yep, well done, missed that.

hello ardav, Andrieux and xan

thank you very much for your replies, and very sorry for my messy post. in fact, this post is my very first post here, and it was posted just before i went to bed, when i was very tired to study how to put a proper post, sorry again.

as your guys said, I was wondering

what's causing the initial ? before the include.

my code is as follow:
1)function which is get data from a table after updating the table:

public function getHtmlWrapper($id) {
//get entire html wrapper
$sql = "SELECT id, theme_name, html_wrapper FROM ".TABLE_PREFIX."themes WHERE id = $id";
$result_id = $this->db->query($sql);
$theme_data = $this->db->fetchToRow($result_id);

//read file in to database
$file_content = file_get_contents(SITE_ROOT.'/themes/'.$theme_data["theme_name"].'/index.php');
if($file_content) {
$this->db->update('themes',
array('html_wrapper'=>$file_content),
"id = '".(int) $id."'");
}

//get entire html wrapper
$sql = "SELECT id, theme_name, html_wrapper FROM ".TABLE_PREFIX."themes WHERE id = $id";
$result_id = $this->db->query($sql);
$theme_data = $this->db->fetchToRow($result_id);
return $theme_data;
}

and html_wrapper gets displayed on this page on the text in red.

<div class="notice">
	Enter your html here. You may use variables here to change the content.
</div>
<form action="<?= site_url('design/edit_wrapper/'.$wrapper['id'].'') ?>" method="POST" name="dataform" onsubmit="if(window.html_wrapper && html_wrapper.textarea.disabled) {html_wrapper.toggleEditor()};">
    <fieldset>
        <legend>Edit HTML wrapper for: <?= $wrapper['theme_name'] ?></legend>
        <? show_error($errors); ?>
		<? show_success($success); ?>
        <div class="edit_page">
               <textarea id="html_wrapper" name="html_wrapper" class="codepress php linenumbers-off autocomplete-off wrap-on" style="width: 98%; height: 500px" /><?= $wrapper['html_wrapper'] ?></textarea>
        </div>
		<input type="hidden" value="<?= $wrapper['id'] ?>" name="id" />
		<p class="buttons">
			<button type="submit" class="button positive">
				Save HTML wrapper
			</button>						
		</p>
    </fieldset>
</form>

the update function in the database class which is: as follow:

function update($table, $data, $where='') {

$q="UPDATE ".TABLE_PREFIX."".$table." SET ";

foreach($data as $key=>$val) {
if(strtolower($val)=='null') $q.= "`$key` = NULL, ";
elseif(strtolower($val)=='now()') $q.= "`$key` = NOW(), ";
else $q.= "`$key`='".$this->escape($val)."', ";
}

$q = rtrim($q, ', ') . ' WHERE '.$where.';';
//echo $q;
return $this->query($q);
}

the escape function below is called by update function

function escape($string) {
if(version_compare(phpversion(),"4.3.0")=="-1") {
return mysql_escape_string($string);
} else {
return mysql_real_escape_string($string);
}
}

when I do var_dump($file_content) before update statement it gives me the contents :

<? include "layout/header.php" ?>
<? $body = (isset($body))?$body:'home'?>
<? include 'contents/'.$body.'.php' ?>
<? include 'layout/footer.php' ?>

but after updating the table with $file_content, and do select the the output is different from $file_content
which gets an extra ? before the <? include "layout/header.php" ?>

?<? include "layout/header.php" ?>

thank you for your help

hello everyone

seems all the web masters have lost interest in reading the long code,

:(

Member Avatar for Member #120589

You've echoed the $q var once. Try updating with escaping with addslashes and html_entities as opposed to mysql_escape functions. Or just try it without any escaping (be careful!). If you still get the '?' you know it's not the escape function.

In addition, it shouldn't really matter, but through your code you've got <? ... ?> and <?= ... ?> . Ensure that you terminate ALL statements with ';' and check again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.