Reason for separating query and result?

Is there any reason why you should separate the query and the result when writing non-readable code?

Example separately:

$query = "SELECT * FROM foo ORDER BY foo2 DESC LIMIT 10";
$result = mysqli_query($dbconnect,$query);

      

compared to one line:

$result = mysqli_query($dbconnect,"SELECT * FROM foo ORDER BY foo2 DESC LIMIT 10"); 

      

I usually use the 1st example, but found myself using the 2nd one line example more and more lately, as its faster and easier to write, so I ask before it gets second nature and then figure out its really bad and could blow the world or something 0.o

+3


source to share


5 answers


It's more of a preference, but readability is definitely a strong justification. However, I would also argue that the scalability and maintainability of the request might be appropriate arguments as well. Let's say you have a complex query with multiple variables that are sanitized for SQL injection with joins as well. In other words, a long query:

$result = mysqli_query($dbconnect,"SELECT * FROM foo, bar Where col1.bar = (Select col1 From someTable where {$varibale} = ...) Group By ... ORDER BY foo2 DESC LIMIT 10");

      



Putting it all in a function makes it difficult to read and anonymity to maintain.

+1


source


To answer this question. Do as you want (or almost).

As I said, the first way is probably used because of the story where we were limited to 80 characters. But this limitation no longer exists (and we have more screens). By the way, I am not telling you to put 300 characters per line.



Use the one you consider yourself more readable / maintainable with. The only downside might be your colleagues. They can dictate which one you should use.

+1


source


In fact, you can write the entire source on one line.

(collapse source = remove all spaces and lines)

certainly not in strings :)

Effect: (1 positive and 3 negative for this)

  • Poor readability (-)
  • Faster parsing (+)
  • Slow edit source (-)
  • No good maintainability (-)

Don't write everything on one line

Effect: (3 positive and 1 negative for this)

  • Good readability (+)
  • Little slow parsing (-)
  • Quick edit source (+)
  • Improved maintainability (+)

(Guess you feel the difference in parsing time in very large code files)

This is how I decided to write my source.

Sometimes (like plugins) I use the collapsed version.

0


source


I think this is also a preference. I usually use the shortcut for short and "unchanging" queries.

But for longer queries, it is easier to read and maintain separately.

Especially for "dynamic queries" (partly depending on external conditions): sometimes it is easier to work with a string, for example, to concatenate a sentence where

or something else, or nothing at all, depending on the condition. The example I mean is a "list.php" script that displays all articles if called without a parameter, and filters articles of a specific category "foo" if called with a get-parameter parameter ?cat=foo

. I personally find the following easier to read and maintain:

$query="select name, description, price from articles";
if(isset($_GET['cat'])) $query.=" where cat={$_GET['cat']}";
$result = mysqli_query($dbconnect,$query);

      

Of course, this can also be done right in the one-liner using ternary operators (?:)

, but then readability suffers a little:

$result = mysqli_query($dbconnect,"select name, description, price from articles".(isset($_GET['cat'])?" where cat={$_GET['cat']}":""));

      

It becomes much more unreadable and overwhelming if you add other conditions. For example, a second filter on get-parameter `? Supplier = bar "...

[EDIT:] I just had a case: when your request fails, it is easier to debug when the request is in line. The simple one echo $query;

lets you see exactly what you are posting to the DB server ...
In the following php code:, $query="select * from $userTable where id=:uid";

you can control what is $userTable

not defined ...
but after that echo $query;

you cannot control what is missing there: $query="select * from where id=:uid";

...

0


source


for me, it's all about readability. I am writing a ton of code. and release the application. 6 months later, when I come back to fix the bug, I need to find things quickly. I highlight as much as possible. Analysis time is minimal unless you write incredibly large amounts of code. its all about readability for me. I am taking one more step to make it as possible as possible.

 $select = "SELECT x,y,z";
 $from = " FROM foo,bar,table3";
 $order = " ORDER BY foo2 DESC ";
 $limit = " LIMIT 10";
 $query = $select . $from . $order . $limit;

 $result = mysqli_query($dbconnect,$query);

      

0


source







All Articles