Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Mayur Pande
PLUS
Mayur Pande
Courses Plus Student 11,711 Points

How to format mysql queries in php

I am using PDO mysql within php. How should the code be formatted?

For example at the moment I am doing this for an insert statement;

$sql = "INSERT INTO `users`(
    `name`,
    `phone`,
    `city`,
    `date_added`)VALUES(
        :name,
        :phone,
        :city,
        :date)";

But I find this is quite horribly formatted. I am using vim so when return to new line after values it indents it like this.

Is the a correct guideline or rule on how to format mysql statements within php

Gabor Gazdag
Gabor Gazdag
10,474 Points

Hi Mayur,

I would like to format your query like this: $sql = "INSERT INTO users(name, phone, city,date_added) VALUES(:name, :phone, :city, :date)"; As it is a short code, just devide the INSERT INTO and the VALUES part in separate rows.

Cheers, Gabor

Vicente Armenta
Vicente Armenta
11,037 Points

Hello,

I have always said that reading code should be like reading a book. It must have some kind of style (that's why there are conventions right?).

Well, you might like something like this:

$query = "
INSERT INTO `users` (`name`, `phone`, `city`, `date_added`)
    VALUES(:name, :phone, :city, :date)";

I get use to this due my heavy work on giant queries.

Anyway, try different formats until you like one or feel comfortable, but remember that you should always be consistent.

Regards.