TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Display Modes
Old 02-20-2008, 09:42 AM   #1 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 349
Thanks: 24
Haris is on a distinguished road
Default Indenting SQL

How do you indent your SQL?

I think there might be a proper standard since Wildhoney separates every clause on different line.
__________________
Necessity is the mother of invention.

My blog
Haris is offline  
Reply With Quote
Old 02-20-2008, 02:01 PM   #2 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 132
Thanks: 3
Gareth is on a distinguished road
Default

sql Code:
SELECT * FROM
    sui_users
WHERE
    username = '$username'
AND
    pass = '$md5password'
Gareth is offline  
Reply With Quote
Old 02-20-2008, 02:22 PM   #3 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

Just hit your <enter> key and use the spacebar to indent. I've not seen a standard per se as to how to properly format SQL statements, but it makes it much more clear (and easy to edit) if it's not all bunched together.
__________________
I reject your reality, and substitute my own.
SOCK is offline  
Reply With Quote
Old 02-20-2008, 03:58 PM   #4 (permalink)
The Wanderer
 
Join Date: Feb 2008
Posts: 10
Thanks: 0
Pete is on a distinguished road
Default

I tend to put a tab line in and start each part of the statment on the tab line so if ofen looks like:
Code:
   $myquery=    "SELECT * FROM table
                WHERE something ='something'"
so like SOCK says but use the tab button, I like to have it in a line so its line1: selecting what, line2: criteria or results, and line3+: my orderby/sortby.

obviously if its a more complex query it changes with joins and what not but thats my basic format for queries.
Pete is offline  
Reply With Quote
Old 02-20-2008, 07:38 PM   #5 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

Now, if you're talking about SQL within a PHP script, I find the overall best solution (not in every case, mind you) is to use HEREDOC syntax, e.g.
PHP Code:

$sql 
= <<< END_SQL
SELECT
 column1
, column2
, date_column
FROM sometable
WHERE pkID = 1024
END_SQL; 
The limitation is that the end statement (in this case END_SQL, can be anything you want) has to be on a non-indented line. So it's great for a script that doesn't have a bunch of conditional blocks that have to be intented. Unfortunately the syntax highlighter has a problem so it doesn't 'pop' as it should, but it works great. Easy to edit, not to mention allowing you the ability to have a HUGE multi-table JOIN that you can actually decipher.

Second best method, IMHO is to simply use PHP's ability to ignore whitespace as Pete pointed out.

PHP Manual Language Reference | Types : Strings | HEREDOC syntax
__________________
I reject your reality, and substitute my own.
SOCK is offline  
Reply With Quote
Old 02-29-2008, 05:19 PM   #6 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

Heredocs aren't great because when you close a heredoc string you must not have any white space in front of it which is a problem is most cases when trying to make your code look clean.
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Old 02-29-2008, 06:07 PM   #7 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 482
Thanks: 51
Alan @ CIT is on a distinguished road
Default

I just tend to split mine over multiple lines if it's a long query to make it more readable than 1 long line.

For example:

Code:
SELECT `a`.`articleid`, `a`.`title`, `a`.`content`, `m`.* 
FROM `articles` AS `a` 
INNER JOIN `authors` AS `m` 
ON a.authorid = m.authorid 
WHERE (m.name = 'Alan') AND (a.created > 1204205135) 
ORDER BY `title` ASC
As long as it's readable it doesn't really matter how you format it

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 02-29-2008, 11:22 PM   #8 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 125
Thanks: 0
wGEric is on a distinguished road
Default

Here's how I do it.

sql Code:
SELECT col1, col2
    FROM TABLE
    WHERE col = 1
        AND col2 = 'foo'
    ORDER BY col3 ASC
    LIMIT 20
__________________
Eric
BlogphpBB
wGEric is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:00 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0