簡體   English   中英

在mysql中,togher用html記錄了php變量。 想要全部獲取並將php變量視為變量,而不是html

[英]In mysql togher with html is recorded php variable. Want to fetch all and treat the php variable as variable, not html

如果我打開php文件,我可以寫例如

Start text 
<?php $var='array from mysql'; 
echo $var;?>. 
Continue text

結果,我將看到Start text array from mysql. Continue text Start text array from mysql. Continue text

想要對mysql做同樣的事情。 但是獲取Start text <?php $var='array from mysql'; echo $var;?>. Continue text Start text <?php $var='array from mysql'; echo $var;?>. Continue text

如何從mysql獲取數據並將php變量視為變量? 安全嗎?

我需要所有這些,因為要在一頁中顯示文本和鏈接列表。 鏈接列表將是動態的(我將寫頁面,鏈接數量將增加)。 因此不要手動添加所有新鏈接,而是要從mysql獲取鏈接URL。 並且鏈接列表要顯示在頁面中的特定位置。 可能還有其他簡單的解決方案,但目前不知道其他解決方案

超鏈接也是如此。 如果在mysql中記錄<a href="topic">Topic name</a>則php會准確地回顯mysql中記錄的內容,而不是超鏈接。

完整的代碼將很長。 但是這里有些簡短(粘貼修改后的代碼)

 $_POST['topic_url'] = '<a href="topic">Topic name</a>';
 //or
 $_POST['topic_url'] = '<?php $var="array from mysql"; echo $var; ?>';

在mysql中記錄

 $stmt_insert = $db->prepare( 'INSERT INTO topics ( TopicUrl ) VALUES (?) ;' );
 $stmt_insert->execute( array( $_POST['topic_url'] ) );

在mysql varchar字段中,我有<a href="topic">Topic name</a>

然后取

 $stmt = $db->prepare("SELECT TopicUrl FROM topics");
 $stmt->execute();
 $content = $stmt->fetch(PDO::FETCH_ASSOC);

然后簡單地回聲

 echo htmlspecialchars( $content['TopicUrl'] , ENT_QUOTES, "UTF-8");

並且在網頁上看到相同的<a href="topic">Topic name</a>

但想查看名為主題名稱的超鏈接

如果echo $content['MainText']; 然后可以看到超鏈接。 但是,如果不是超鏈接,則在mysql中記錄此

<?php $var="array from mysql"; 
echo $var;
?>

然后回聲,什么也看不見。

看來您這里有幾個問題。 為了在php中正確呈現html,您需要包含header,html和body標簽。 如果在代碼周圍添加這些內容,則超鏈接應在瀏覽器中正確呈現。

我看到的另一個主要問題是您應該將每個條目的url和名稱分別存儲在數據庫中。 html部分不應位於數據庫中。 您可以使用php函數包裝每一行,但一定不要像這樣存儲完整的html行。 幫自己一個忙,為將來而努力,研究包括第三范式在內的數據庫設計。

格式化信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM