For anyone still using the old "mysql_fetch_assoc" here is something I've just discovered today after about 9 years of PHP'ing and thought I should sahre because I've not seen many open source projects use it (maybe for another reason I'm not yet aware of)...

(note: both provide the exact same output)

if ($no_of_records > 0) {
    $overhead_info = mysql_fetch_assoc($sql_result);
    extract($overhead_info);
}
echo "$rid, $type, $summary, $details, $cost, $purchase_date";


############  instead of  ###############


if ($no_of_types > 0) {
	while ($overhead_type_info = mysql_fetch_assoc($sql_result))
	{
		$rid = $overhead_type_info['row'];
		$type = $overhead_type_info['type'];
		$summary  = $overhead_type_info['summary'];
		$details  = $overhead_type_info['details'];
		$cost  = $overhead_type_info['cost'];
		$purchase_date = $overhead_type_info['purchase_date'];
	}
	echo "$rid, $type, $summary, $details, $cost, $purchase_date";
}
pbcomput commented: Grt...Hats off...Thanks so much... :) +1

Recommended Answers

All 14 Replies

You can not do anything without loop.
Correct code is as follows

if ($no_of_types > 0) {
	while ($overhead_type_info = mysql_fetch_assoc($sql_result))
	{
		extract($overhead_type_info);
		echo "<br>$rid, $type, $summary, $details, $cost, $purchase_date";
	}

}

there is no problem with the code if you are only trying to extract the first result or only 1 result. this post was a tip... not a problem :-)

(note: both provide the exact same output)

I posted because of above quote and just to save newbies from confusion.

I posted because of above quote and just to save newbies from confusion.

if there was an edit button i'd ammend it to say "provides exact same output if there's only 1 result". i got too excited and assumed most people would have known what iw as talking about haha

hi kevindougans its nice... Really its useful one...

thanx...

For anyone still using the old "mysql_fetch_assoc" here is something I've just discovered today after about 9 years of PHP'ing and thought I should sahre because I've not seen many open source projects use it (maybe for another reason I'm not yet aware of)...

(note: both provide the exact same output)

if ($no_of_records > 0) {
    $overhead_info = mysql_fetch_assoc($sql_result);
    extract($overhead_info);
}
echo "$rid, $type, $summary, $details, $cost, $purchase_date";


############  instead of  ###############


if ($no_of_types > 0) {
	while ($overhead_type_info = mysql_fetch_assoc($sql_result))
	{
		$rid = $overhead_type_info['row'];
		$type = $overhead_type_info['type'];
		$summary  = $overhead_type_info['summary'];
		$details  = $overhead_type_info['details'];
		$cost  = $overhead_type_info['cost'];
		$purchase_date = $overhead_type_info['purchase_date'];
	}
	echo "$rid, $type, $summary, $details, $cost, $purchase_date";
}

Grt.....:cool: Thanks for such info....:)

One more thing i want to share that i also tried this wid

mysql_fatch_array()

wel no dought its also working wid this also...:icon_biggrin:

n i already starting to use this one.....:icon_biggrin::icon_cool:

I guess what I don't understand is why would someone break up an array into other variables? you already have values in the array. I mean it's cool to know and I didn't even know it existed, I think because I've just never needed it.

Unless, I am working with preexisting code and I need to make a page more dynamic and I don't feel like doing a find and replace, but that would be just lazy.

it really helped me with a current project i've just started working on because of the self made MCV structure they have setup means they use all the column names from the database.

E.g. they have a page to display everything and it's like :

$html_output .=<<<END
			
			  <table style="width: 100%;" cellpadding="5" cellspacing="5" border="0">						
					<tr>
					  <th class="forms_th" style="width: 20%;">Overhead for job:</th>
					  <td style="width: 80%;">$rid</td>
				  </tr>
				  <tr>
					  <th class="forms_th" style="width: 20%;">Unique Overhead ID:</th>
					  <td style="width: 80%;">$eid $quote_ref</td>
				  </tr>
				  <tr>
					  <th class="forms_th" style="width: 20%;">Overhead Type:</th>
					  <td style="width: 80%;">$overhead_type $form_req</td>
				  </tr>
				    <tr>
					  <th class="forms_th" style="width: 20%;">Date:</th>
					  <td style="width: 80%;">$purchase_date</td>
				  </tr>

			  </table>

END;

and then instead of looping through the results and setting each variable I can now just do extract.... simple :-)

Extract is almost always bad practice, especially in the use case provided. Case in point: You worked on some code at a job and it uses extract and you leave the job and someone else comes along and inherits the code. "Hmm, there's this bug involving $somevar but I don't see it defined anywhere..."

3 hours later extract($some_random_array); // I'm awesome "FFFFUUUUUUUU"

Extract is almost always bad practice, especially in the use case provided. Case in point: You worked on some code at a job and it uses extract and you leave the job and someone else comes along and inherits the code. "Hmm, there's this bug involving $somevar but I don't see it defined anywhere..."

3 hours later extract($some_random_array); // I'm awesome "FFFFUUUUUUUU"

That's why you should comment your code clearly :-)

That's why you should comment your code clearly :-)

Comments or not, extract is by definition obfuscating the initialization of multiple variables. In addition using extract on superglobals (_GET, _POST, etc.) is a massive security risk (You didn't provide this in your example, I'm just pointing out that wanton use of extract is dangerous)

Aren't you sure you've misspelled that?

Did you really mean ...

mysql_fetch_array()

Grt.....:cool: Thanks for such info....:)

One more thing i want to share that i also tried this wid

mysql_fatch_array()

wel no dought its also working wid this also...:icon_biggrin:

n i already starting to use this one.....:icon_biggrin::icon_cool:

That's why you should comment your code clearly :-)

Dude, let me tell you. If I ever used extract in my current position I would feel like an idiot.

Mainly because it automatically doubles your memory usage, that is unless it destroys the array upon doing it. But not only that, it show a lack of understanding in how to use arrays.

Comments or not, extract is by definition obfuscating the initialization of multiple variables. In addition using extract on superglobals (_GET, _POST, etc.) is a massive security risk (You didn't provide this in your example, I'm just pointing out that wanton use of extract is dangerous)

can u pls tell me y it should b dangerous ?? bcz i started workin like that in my projects...

Thanks..:)

-----

@ JuanMan

Its just a typo...yeah m want to tell that only...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.