简体   繁体   English

从数据库中提取Javascript

[英]Pulling Javascript from database

I have a database which contains code with different languages. 我有一个数据库,其中包含使用不同语言的代码。 These languages include HTML, javascript and PHP. 这些语言包括HTML,javascript和PHP。 I want to pull this code stored in the database and display it in a textarea. 我想提取此代码存储在数据库中,并在文本区域中显示它。 This works fine for HTML the data is pulled and is presented fine. 这对于HTML可以很好地工作,并且可以很好地呈现数据。 When the database contains some javascript such as: 当数据库包含一些javascript时,例如:

 <script>
 var a = 0;
 var word = "";
 </script>

No code is displayed in the database. 数据库中未显示任何代码。 This is my code for pulling the code from the database and displaying: 这是我的代码,用于从数据库中提取代码并显示:

<?php       
$sql = "SELECT Line_Code FROM Code_Stream1";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){
        printf("%s \n", $conn->real_escape_string($row['Line_Code']));

    }
}else{
    echo "no code";
}
?>

Also, when an entry is empty instead of the text editor just skipping to the next line a newline character (\\n) is entered. 同样,当条目为空而不是文本编辑器时,仅跳至下一行,将输入换行符(\\ n)。

In short, HTML is pulled and displayed fine but JS is not 简而言之,HTML被拉出并显示正常,但JS却没有

Don't use $conn->real_escape_string() to escape code being displayed. 不要使用$conn->real_escape_string()转义所显示的代码。 Use htmlentities() to translate characters that have special meaning in HTML to the corresponding entities, so they won't be rendered as HTML markup. 使用htmlentities()将HTML中具有特殊含义的字符转换为相应的实体,因此它们不会被呈现为HTML标记。

printf("%s \n", htmlentities($row['Line_Code']));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM