mysqlFor programmers who are starting with mysql a couple of tips to make life easier. A small list of basic tips beside the most important one: Use an error handler!

 

 

  1. Look to the future, not to the easiest way
    Use PDO or another extension so you can also use your applications with php > 6.0
  2. Never, but I mean NEVER use ` (back ticks)
    Choose the fieldnames in the database wisely. Take a look at the list of reserved words for MySql and you’ll know wich words you don’t have got to choose.
  3. Choose the correct field type
    When you save a date, choose a DATE filed, when you have a decimal choose DECIMAL and so on…
  4. Destroy, blowup and sink the functions DATE_ADD() and DATE_SUBB()
    Use the more logical INTERVAL when you’re calculating with dates. Read more about the MySql time and date functions.
  5. Write clean handy query’s
    I know I don’t do this on this website but that’s because of the syntax highlighter.
//A not-clean query.
$sql = "SELECT t.id, t.name, t.cat FROM table AS t INNERJOIN ctable AS c ON t.cat = c.id WHERE t.cat = 1 AND t.name = 'Testdata' ORDER BY t.name ASC LIMIT 0,10";

//A clean one. Note that the syntax highlighter is struggeling with this one and on this blog the 'dirty one' is used...
$sql = "SELECT
			t.id,
			t.name,
			t.cat
		FROM
			table AS t
		INNERJOIN ctable AS c ON t.cat = c.id
		WHERE
			t.cat = 1
		AND
			t.name = 'Testdata'
		ORDER BY
			t.name ASC
		LIMIT 0,10";

Related Posts

Leave a Comment

Get your own Gravatar!
Your email will never be published!

Notify me of followup comments via e-mail

Top of Page