6

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. So I will have categories and then pages with sample code that I write with a javascript syntax highlighter. Instead of storing my php code snippets in the file I am wanting to save them to mysql DB. So what is the best way to save PHP into mysql and to get it out of mysql to show on the page?

My end result will be something like this
alt text http://img2.pict.com/c1/c4/69/2516419/0/800/screenshot2b193.png


Update:

I just wasn't sure if I needed to do something special to the code before sending it to mysql since it has all different kinds of characters in it

JasonDavis
  • 48,204
  • 100
  • 318
  • 537
  • er... did you know that PHP has a built-in syntax highlighter? http://php.net/highlight-string – Powerlord Jan 15 '10 at 18:39
  • I don't understand your question. Are you asking how to store and retrieve something from a MySQL database? – Ian P Jan 15 '10 at 18:28

6 Answers6

11

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

Sampson
  • 265,109
  • 74
  • 539
  • 565
  • 1
    Is `mysql_real_escape_string` required with mysql `text` datatype? – knoxgon Mar 10 '17 at 21:46
  • You should escape it before inserting to the db, so it will prevent it from being causing error. For example, php also prints variables between text (double quotes, or single quotes) Like `` –  Aug 25 '22 at 09:51
4

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

JW.
  • 50,691
  • 36
  • 115
  • 143
2

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.

Asaph
  • 159,146
  • 25
  • 197
  • 199
  • Not downvoting. But if this is true then it's a shame that in MySQL only MyISAM supports full text indexes. This means you'll have to lose transactions, referential integrity ... – ChristopheD Jan 15 '10 at 18:31
  • @ChristopheD: So true. It would be nice to see `fulltext` support in InnoDB. – Asaph Jan 15 '10 at 18:33
0

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.)

Blank
  • 7,088
  • 12
  • 49
  • 69
0

Store as text (varchar) in the database.

Use cascading style sheet (css) to format code.

http://qbnz.com/highlighter/

Yada
  • 30,349
  • 24
  • 103
  • 144
0

Try this:

mysql select ...

eval('?>' .  $row['phpcode'] . '<?php ');
McDowell
  • 107,573
  • 31
  • 204
  • 267