Jump to content

[SOLVED] How can I condense this?


jamesbrauman

Recommended Posts

I've been looking for a way to condense these two lines of code into one for a while now, couldn't find anything. If I have code such as:

$row = mysql_fetch_array(mysql_query("SELECT * FROM example_table WHERE id=$example_id"));
$data = $row['data'];

 

I thought about doing this, however it does not seem to work:

$data = mysql_fetch_array(mysql_query("SELECT * FROM example_table WHERE id=$example_id"))['data'];

 

Is there any way I can condense those two lines down to one? It seems I am declaring an unnessessary variable there ($row).

 

Link to comment
https://forums.phpfreaks.com/topic/124467-solved-how-can-i-condense-this/
Share on other sites

Nope, not possible.

Think about it, the variable isn't unnecessary. The variable is in fact holding an array with all the information you require.

There is no need to put them in the one line.

 

If you really, really want to save lines then just do it like this:

 

$row = mysql_fetch_array(mysql_query("SELECT * FROM example_table WHERE id=$example_id")); $data = $row['data'];

 

But it's pointless.

You could do this using the list() function (its' under the array functions in the manual) since mysql_fetch_array returns an array.

 

However, you are going the wrong way in trying to condense this, if you are writing code that will be used outside of a programming class. Real life code needs a maximum of error checking, error reporting, error logging, and error recovery logic so that it will perform as expected when anything unexpected occurs.

 

If you want to write condensed code (one statement that executes several lines of code to produce a value), use functions or classes.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.