Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I store PHP code inside of a mysql table

Tags:

php

mysql

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

like image 488
JasonDavis Avatar asked Jan 15 '10 18:01

JasonDavis


Video Answer


3 Answers

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

like image 192
Sampson Avatar answered Sep 22 '22 21:09

Sampson


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

like image 31
JW. Avatar answered Sep 19 '22 21:09

JW.


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.

like image 24
Asaph Avatar answered Sep 21 '22 21:09

Asaph