jamesbrauman Posted September 16, 2008 Share Posted September 16, 2008 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 More sharing options...
JasonLewis Posted September 16, 2008 Share Posted September 16, 2008 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. Link to comment https://forums.phpfreaks.com/topic/124467-solved-how-can-i-condense-this/#findComment-642749 Share on other sites More sharing options...
jamesbrauman Posted September 16, 2008 Author Share Posted September 16, 2008 Yeah, I would just would have be able to do it on one line, so when I need to get one value from one field in my mysql database, I can just go: $data = (data_from_mysql_database); $other_data = (data_from_mysql_database); Ah well. Thanks. Link to comment https://forums.phpfreaks.com/topic/124467-solved-how-can-i-condense-this/#findComment-642761 Share on other sites More sharing options...
JasonLewis Posted September 16, 2008 Share Posted September 16, 2008 You can use mysql_result() as well. It can come in handy for many things. Link to comment https://forums.phpfreaks.com/topic/124467-solved-how-can-i-condense-this/#findComment-642763 Share on other sites More sharing options...
PFMaBiSmAd Posted September 16, 2008 Share Posted September 16, 2008 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. Link to comment https://forums.phpfreaks.com/topic/124467-solved-how-can-i-condense-this/#findComment-642766 Share on other sites More sharing options...
jamesbrauman Posted September 16, 2008 Author Share Posted September 16, 2008 Thanks both of you. mysql_result() was exactly the function I was looking for. Link to comment https://forums.phpfreaks.com/topic/124467-solved-how-can-i-condense-this/#findComment-642768 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.