newbtophp Posted April 13, 2010 Share Posted April 13, 2010 I know the typical variables when extracting from db are $row, $result, $sql, but what about if you was to extract from various tables and you don't want the variables to interfer with each other (such as overwrite), what would you name them like? (I know they can be named anything random, was thinking more generic?? (not sure if thats the right word to describe)) Example scenario (extract): $result_2 = mysql_query("SELECT * FROM site_posts WHERE post_author = '{$user}' GROUP BY post_body ORDER BY post_id DESC LIMIT 5"); if(mysql_num_rows($result_2)) { while ($row_2 = mysql_fetch_array($result_2)) { $date = ago($row_2['post_date']); $forum_number = $row_2['post_forum']; $topic_number = $row_2['post_topic']; $post = $row_2['post_body']; $post_id = $row_2['post_id']; $result_3 = mysql_fetch_array(mysql_query("SELECT * FROM site_forums WHERE forum_id = '{$forum_number}'")); if($result_3['view_level'] == 'user') { $result_4 = mysql_query("SELECT * FROM site_topics WHERE topic_id = '{$topic_number}'"); while($row_3 = mysql_fetch_array($result_4)) { $topic_title = $row_3['topic_name']; Wanting to hear your thoughts, on how you'd do this. Quote Link to comment https://forums.phpfreaks.com/topic/198445-how-would-your-structurename-your-variables/ Share on other sites More sharing options...
trq Posted April 13, 2010 Share Posted April 13, 2010 Executing queries within loops is a bad idea to begin with. You can likely achieve the same results more efficiently using an sql JOIN and one query. Quote Link to comment https://forums.phpfreaks.com/topic/198445-how-would-your-structurename-your-variables/#findComment-1041323 Share on other sites More sharing options...
andrewgauger Posted April 13, 2010 Share Posted April 13, 2010 Try meaningful names, such that if you didn't see what it was assigned you could defer what the value was, there are ways to do this. I might go as far as: $result_site-posts_by_author=mysql_query("SELECT * FROM site_posts WHERE post_author = '{$user}' GROUP BY post_body ORDER BY post_id DESC LIMIT 5"); Quote Link to comment https://forums.phpfreaks.com/topic/198445-how-would-your-structurename-your-variables/#findComment-1041330 Share on other sites More sharing options...
newbtophp Posted April 13, 2010 Author Share Posted April 13, 2010 Executing queries within loops is a bad idea to begin with. You can likely achieve the same results more efficiently using an sql JOIN and one query. I see, but beside the while loops issue, what kinda of variable names would you assign them, contiguous? Quote Link to comment https://forums.phpfreaks.com/topic/198445-how-would-your-structurename-your-variables/#findComment-1041337 Share on other sites More sharing options...
newbtophp Posted April 13, 2010 Author Share Posted April 13, 2010 Try meaningful names, such that if you didn't see what it was assigned you could defer what the value was, there are ways to do this. I might go as far as: $result_site-posts_by_author=mysql_query("SELECT * FROM site_posts WHERE post_author = '{$user}' GROUP BY post_body ORDER BY post_id DESC LIMIT 5"); Thanks, i'll see what others think and decide, but you can't have dashes within variables Quote Link to comment https://forums.phpfreaks.com/topic/198445-how-would-your-structurename-your-variables/#findComment-1041338 Share on other sites More sharing options...
ddubs Posted April 13, 2010 Share Posted April 13, 2010 I usually go w/ lowercase(first) word then proper case on any words there-after. Example: $numTopics $numRegUsers ...etc Quote Link to comment https://forums.phpfreaks.com/topic/198445-how-would-your-structurename-your-variables/#findComment-1041341 Share on other sites More sharing options...
andrewgauger Posted April 14, 2010 Share Posted April 14, 2010 I actually have my own variable style: I use a single capital leter followed by an underscore and then camel case descripting: $Q_userId (query, user table id column) $A_intAge (Argument for a function, integer age) Quote Link to comment https://forums.phpfreaks.com/topic/198445-how-would-your-structurename-your-variables/#findComment-1041425 Share on other sites More sharing options...
ignace Posted April 14, 2010 Share Posted April 14, 2010 I might go as far as: $result_site-posts_by_author=mysql_query("SELECT * FROM site_posts WHERE post_author = '{$user}' GROUP BY post_body ORDER BY post_id DESC LIMIT 5"); Will throw an error as you wrote: null-undefined=resource Quote Link to comment https://forums.phpfreaks.com/topic/198445-how-would-your-structurename-your-variables/#findComment-1041492 Share on other sites More sharing options...
andrewgauger Posted April 14, 2010 Share Posted April 14, 2010 Yep, let go of the shift key too soon. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/198445-how-would-your-structurename-your-variables/#findComment-1041722 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.