Naming Conventions - Work with PHP and MySQL

Aloha

I just started programming in PHP recently and had a basic naming question. I read a lot of PHP coding standards after reading this post . This made sense to me, as I already prefer to use camelCase when naming variables. Since I'm going to work with MySQL, I also read the database naming conventions. There are many different preferences, but it seems that as long as you agree, everything is in order. However, one thing that I thought I noticed was that most database naming conventions talk about the beginning of attributes with an uppercase letter, whereas in PHP (as in most programs) variables should start with lowercase letters. I am going to use the MVC model with DAO and VOs. I really want my VOs to reflect my database tables, but the shell of the first letter is inconsistent. Should I leave the housing incorrectly matched or change one so that they are the same? How do you do this?

Thanks in advance,

Frank

+7
php mysql naming-conventions
source share
5 answers

I would suggest using lower case with underscores to name the database as this will make your code more flexible.

Example:

Suppose you use MySQL as a database and name the table FooBar. Now the client wants to switch to PostgreSQL as an alternative database, this leads to the fact that the name of your table becomes a problem, because postgres uses lower case names or is wrapped in double quotes.

MySQL query syntax:

SELECT * FROM FooBar 

Postgres request syntax:

 SELECT * FROM "FooBar" 

If you followed the general naming scheme, you could use one query for

 SELECT * FROM foo_bar 
+5
source share
+2
source share

Ya, for database and table, everyone prefers underscore. Few people like all uppercase and a few lowercase. But if Phil is correct, I will vote for all lowercase letters for the database and table.

+1
source share

do it as you like, but as you yourself said:

[...] as long as you agree, this is normal.

I would prefer to use the same case for both and start with a lowercase character.

0
source share

I prefer to use all lower case in table and column names, and spaces instead of underscores. The name of the table in the singular of the model name. This usually helps to transform the database structure into a standard PEAR model.

 class User extends MyActiveRecord { public function getFirstName() { //... } } SELECT * FROM user WHERE first_name LIKE 'Bill' 

Foreign keys as "some specific information" _ "foreign table name" _ "column name". This helps to find out in which table we are referring to the foreign key.

 SELECT * FROM post LEFT JOIN user ON post.author_user_id = user.id 
0
source share

All Articles