I'm creating a codex that will store example of functions and examples on how to use functions. Can I store this information in a database? Also, is there a proper way of displaying code on a page?
The short answer is yes.
The longer answer is yes and I would use a text data type to store the text of the code. The relevant encoding function like mysqli_real_escape_string should make sure that you do not have malformed queries when you insert the code example.
When it comes to displaying it later you could use the highlight string function to output pretty looking code that is easy on the eyes. http://php.net/manual/en/function.highlight-string.php
If you add a FULLTEXT index on the field then you could even search for entries by searching the code itself.