简体   繁体   English

如何在MySQL数据库中插入PHP代码段

[英]How to insert a php code snippet in mysql database

I have a php code snippet like below : 我有一个如下的php代码片段:

function is_ancestor_of( $page_name = null, $post ) {

if(is_null($page_name))
return false;

// does it have a parent?
if (!isset( $post->post_parent ) OR $post->post_parent <= 0 )
return false;

//Get parent page
$parent = wp_get_single_post($post->post_parent);

 if ( $parent->post_name == $page_name ){
echo $parent->post_name.' - '.$page_name;
return true;
} else {
is_ancestor_of( $page_name, $parent );
}
}

I just want to insert this whole code in mysql database table and again retriew it with this same formatting on the page again. 我只想将整个代码插入mysql数据库表中,然后再次在页面上以相同的格式重新检索它。 When I simply insert this code in database using INSERT INTO query then the mysql syntax error occurs because the code breaks sql query because of special characters. 当我仅使用INSERT INTO查询将此代码插入数据库时​​,由于代码因特殊字符而中断了sql查询,因此会发生mysql语法错误。 Is there any way to do this?? 有什么办法吗?

Thanks. 谢谢。

Escape special characters using mysql_real_escape_string() . 使用mysql_real_escape_string()转义特殊字符。 You'd probaply be better off moving away from mysql_* functions though and start using PDO or mysqli_* for example. 不过,最好还是远离mysql_ *函数,而开始使用PDOmysqli_ *

Edit As mentioned in the comments, make sure you place the code as a string and that the DB field is the correct data type. 编辑如注释中所述,请确保将代码放置为字符串,并且DB字段是正确的数据类型。 Also, make sure you use mysql_real_escape_string() (if you insist on using mysql_*) on the whole string (or code). 另外,请确保对整个字符串(或代码)使用mysql_real_escape_string()(如果您坚持使用mysql_ *)。

Yeah, you can do this. 是的,您可以这样做。 Multiple ways: 多种方式:

  1. PDO - that's the best option actually. PDO-实际上,这是最佳选择。 Study this topic in the PHP manual as this can be a lot more help than just protecting your MySQL queries in PHP from breaking. 在PHP手册中研究此主题,因为它不仅可以保护PHP中的MySQL查询免受破坏,还可以提供更多帮助。
  2. addslashes() - adds slashes to escape characters. addlashes()-添加斜杠以转义字符。 This won't break your MySQL query for sure. 这肯定不会破坏您的MySQL查询。
  3. mysql_real_escape_string() mysql_real_escape_string()
  4. mysql_escape_string() 函数mysql_escape_string()

EDITED Emphasized PDO as compared to the previous version of this answer as PDO is far more safer than the other options and there is a lot more to it which can be used while working with PHP and MySQL. EDITED强调PDO相比以前的版本这个答案为PDO是远远超过其他选项更安全,也给了很多更可与PHP和MySQL工作时使用。

You will need to use mysql_real_escape_string() to escape the php code so it does not throw an error when inserting. 您将需要使用mysql_real_escape_string()来转义php代码,以便在插入时不会引发错误。 Then you run an eval() on the statement, if you want it to execute. 然后,如果要执行该语句,请对该语句运行eval()。 If you have a mixed html and php stored in the database you would call eval like so 如果您在数据库中存储了混合的html和php,则可以这样调用eval。

 eval('?>'.$dbresult.'<?php');

Just make sure you stripslashes() on the database result 只要确保您在数据库结果上加上stripslashes()

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

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