-

Intro to databases

Typically, eventually, anyone using php will at some point use it in conjunction with a database server and in most cases, this database server will be MYSQL.

What Are the advantages of using a database server?

The advantages of a database server are multiple:

  • Queries can be made to data in a much faster way, especially when there are many concurrent queries
  • It is easier to scale a database server as needed, using replication as needed.
  • sql syntax is relatively standard and allows for portable code across database servers
  • data can be accessed easily through apis in multiple programming environments
  • there is a security api included with the sql server which allows for better data security as well as integrity through things such as transactions

Things to be aware of when using a database server

Database servers are powerful and used well can bring awesome data crunching capabilities. It is however important to do careful planning or it may easily cripple your web app, especially on a high performance website.

Here are a couple of tips:

  • if data is going to be used multiple times, data caching can speed up very easily web page access as well as decrease processor resources being hit
  • if many queries are to be done, a well placed index can decrease page processing speed as well as optimise resource use.
  • If many inserts / deletes/ updates are to be done, using transactions can greatly improve performance
  •  There are cases where a simple flat text file can do the job faster and in a much easier way
  • Using a GUI tool like phpmyadmin can make your life a lot easier by providing CRUD functionality (create read update delete) through a graphical interface to databases. It also makes creation, editing of databases themselves easier and less error prone. Make sure you password phpmyadmin access if the server is going to be publically accessible, Preferably, do not put it in the default /phpmyadmin/ path to maximise security.

It is also interesting to note that in many cases (especially if the queries are simple), when a mysql server is not available sqlite can be an excellent alternative providing fast performance and small memory foot print… sometimes very easily by simply slightly changing the function names (which are very similar).

 If sqlite is used, make sure the database files are protected from unauthorized web access either by putting them outside the web server document root or with an appropriate htaccess entry.

Notes about queries

  • Mysql queries end in a semi column or in the monitor, with g
  • Several queries can be put in one string