Jump to content

How would your structure/name your variables?


newbtophp

Recommended Posts

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.  :)

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

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

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  :P

  Quote

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

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.