johnny_s johnny_s - 1 year ago 110
MySQL Question

PHP query structure

I am trying to learn object oriented mysqli and I have a (pretty basic I'm sure) question. I am following the guide here

When it comes to writing queries, what's the difference (if any) between the following queries;


$sql = <<<SQL
FROM `users`
WHERE `live` = 1


$sql = ("SELECT * FROM users WHERE live = 1");

I would like to start as I mean to go on so any advice is appreciated.

Answer Source

The first is heredoc syntax: heredoc

They both serve the same purpose. Using heredoc makes it easier to mix static text with variables without having to worry about string concatenation or using {$variable} to have variables inside strings.

Using heredoc also allows multilining your queries for easier reading in a cleaner way.

Disclaimer: I don't use heredoc extensively, but for lengthy queries it makes it easier to break down in lines and read them :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download