2011-11-10 131 views
0

我的技術博客沒有起步,因爲Wordpress無法保存包含PHP,HTML和SQL代碼的帖子。我必須修改Wordpress衛生方法或推出我自己的基本博客平臺,但在MySQL數據庫中使用這種內容存儲技術文本的正確和安全的方法是什麼?將SQL存儲在MySQL數據庫中

回答

0

我假設你使用的是PHP。
最好是使用PDO,見:http://www.php.net/manual/en/pdo.prepare.php

從該網站
實例#1,準備一個SQL語句命名參數

<?php 
/* Execute a prepared statement by passing an array of values */ 
$sql = 'SELECT name, colour, calories 
    FROM fruit 
    WHERE calories < :calories AND colour = :colour'; 
$sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY)); 
$sth->execute(array(':calories' => 150, ':colour' => 'red')); 
$red = $sth->fetchAll(); 
$sth->execute(array(':calories' => 175, ':colour' => 'yellow')); 
$yellow = $sth->fetchAll(); 
?> 

例2準備與問號參數的SQL語句

<?php 
/* Execute a prepared statement by passing an array of values */ 
$sth = $dbh->prepare('SELECT name, colour, calories 
    FROM fruit 
    WHERE calories < ? AND colour = ?'); 
$sth->execute(array(150, 'red')); 
$red = $sth->fetchAll(); 
$sth->execute(array(175, 'yellow')); 
$yellow = $sth->fetchAll(); 
?> 

或者使用轉義

使用mysql_real_escape_string()可以在將值插入查詢之前轉義值。

$val1 = mysql_real_escape_string($_POST['value1']); 
$query = "SELECT a,b,c FROM table1 WHERE a = '$val1' "; 
// the quotes are essential    ^ ^

1.4.3代碼代碼
當輸出內容的使用:

echo "the output is: ".htmlentities($output); 
+0

我期待存儲在包含PHP,JS,HTML,SQL數據庫中的文本內容(網頁)該內容中的代碼。在SO上代碼示例如下。 WordPress不喜歡它,所以需要修改/編寫我自己的衛生設施,以便將所有代碼示例安全地保存在MySQL數據庫字段 – MattP

+0

@MattP您需要的**唯一**衛生是'mysql_real_escape_string()'。 – Johan