4

Now I got a query that contains HTML code and when I select it in php it makes the code,

$query = mysql_query("SELECT * FROM `table`")or die(mysql_error());
while($arr = mysql_fetch_array($query)){
$num = mysql_num_rows($query);
$code = $arr ['code'];
echo $code;
}

and in that query there is this code <a href="http://google.com">Click Here</a>

when it echo it shows me Click Here, But I want it to sows me the code.

So how can I do that in PHP.

Thanks
Klaus

1

4 Answers 4

5

Use htmlspecialchars() to prevent HTML elements from being interpreted by the browser.

$query = mysql_query("SELECT * FROM `table`")or die(mysql_error());
while($arr = mysql_fetch_array($query)){
    $num = mysql_num_rows($query);
    $code = $arr ['code'];
    echo htmlspecialchars($code);
}
Sign up to request clarification or add additional context in comments.

Comments

4

Just encode the output with htmlspecialchars

$query = mysql_query("SELECT * FROM `table`")or die(mysql_error());
while($arr = mysql_fetch_array($query)){
$num = mysql_num_rows($query);
$code = $arr ['code'];
echo htmlspecialchars($code);
}

Comments

2

You can use htmlentities:

echo htmlentities($code);

This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.

Comments

1

Simply use:-

echo htmlentities($code);

The htmlentities() function takes a string and returns the same string with HTML converted into HTML entities.

It prevents the browser from using it as an HTML element and it prevents the code from running if you were to display some user's input on your website.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.