How can I store PHP code inside of a mysql table

后端 未结 6 1962
小蘑菇
小蘑菇 2020-12-11 06:42

I am working on building a small php/mysql script that will act something like a wordpress blog but will just be a small site for my eyes only to store PHP code snippets. S

相关标签:
6条回答
  • 2020-12-11 07:23

    Just store in a text field, as is. Not much more beyond that.

    0 讨论(0)
  • 2020-12-11 07:29

    Try this:

    mysql select ...
    
    eval('?>' .  $row['phpcode'] . '<?php ');
    
    0 讨论(0)
  • 2020-12-11 07:33

    If you're not using some kind of database abstraction layer, just call mysql_real_escape_string on the text.

    0 讨论(0)
  • 2020-12-11 07:38

    Store as text (varchar) in the database.

    Use cascading style sheet (css) to format code.

    http://qbnz.com/highlighter/

    0 讨论(0)
  • 2020-12-11 07:41

    Do you want to be able to search the php code? If so, I recommend using the MyISAM table type as it supports full text indexes (InnoDB does not). Your choices for column type when it comes to a fulltext index are char, varchar and text. I would go with text as your code snippets might get too long for the other types.

    Another point worth mentioning, is make sure you properly escape all php code (or any value for that matter) before you insert it. The best way to do this is by using parameterized queries.

    0 讨论(0)
  • 2020-12-11 07:46

    Unless I'm missing part of the problem, you should be safe storing it as a TEXT field in a MySQL database. Just make absolutely sure you sanitize the code snippets, as PHP code in particular is quite likely to contain the characters that will escape out of an SQL string. (If you're already using an SQL framework, odds are the framework is doing this for you.)

    0 讨论(0)
提交回复
热议问题