Jump to content

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");

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?

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

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)

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.