Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> The Extract() Function, Something I just found out
ghostrider
post Feb 24 2007, 09:47 PM
Post #1


Super Member
*********

Group: Members
Posts: 397
Joined: 9-June 06
From: Wisconsin
Member No.: 24,924



The extract() function is used in PHP to take an array and split it up into variables. MySQL queries can be parsed this way. Below is an example.

CODE
$query = mysql_query("select username, password from users where uid=1");
$result = mysql_fetch_array($query, mysql_assoc);
extract($result);
print "Your username is : $username";


The extract() function works for ANY array, including $_POST, and $_GET. Makes processing form data a LOT easier biggrin.gif .
Go to the top of the page
 
+Quote Post
shadowx
post Feb 24 2007, 10:44 PM
Post #2


A clever man learns from his own mistakes, a WISE man learns from those of OTHERS
*********

Group: [HOSTED]
Posts: 975
Joined: 12-April 06
From: Essex, UK
Member No.: 21,719



I might be able to make use of this, but first, i dont fully understand. Say i had an array like this:

QUOTE
$array
>>username = shadowx
>>password = mypass
>>ip = 111.111.111.111


by that i mean that $array['username'] is "shadowx"

Now if i were to use EXTRACT would it give me three variables called $username, $password and $ip or do i have to say what i want it to call the variables somehow?
Go to the top of the page
 
+Quote Post
jlhaslip
post Feb 24 2007, 11:00 PM
Post #3


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 3,993
Joined: 24-July 05
From: In Trouble Again... still?
Member No.: 9,787
Spam Patrol



Looks like it would work on any associative array.

http://ca.php.net/manual/en/function.extract.php
Go to the top of the page
 
+Quote Post
ghostrider
post Feb 25 2007, 01:57 AM
Post #4


Super Member
*********

Group: Members
Posts: 397
Joined: 9-June 06
From: Wisconsin
Member No.: 24,924



You have it right shadowx. It calls the variable names the names of the elements in the array. I would assume it wouldn't work on numerical arrays, but I'm not sure.
Go to the top of the page
 
+Quote Post
rvalkass
post Feb 25 2007, 01:06 PM
Post #5


apt-get moo
Group Icon

Group: [MODERATOR]
Posts: 2,111
Joined: 28-May 05
From: Hertfordshire, England
Member No.: 7,593
Spam Patrol



It is very useful, but use it with care. People can easily overwrite variables in a string of $_GET variables passed by the browser. You could assign one thing, then they just reassign it at the end. People can also overwrite variables not assigned in the $_GET but that you have been using anyway. Therefore, if you are going to use it, call it before you create any variables (so they have nothing to overwrite) and use the EXTR_SKIP flag to skip any variables that have already been assigned, which stops them tagging them on the end of the address.
Go to the top of the page
 
+Quote Post
shadowx
post Feb 25 2007, 03:12 PM
Post #6


A clever man learns from his own mistakes, a WISE man learns from those of OTHERS
*********

Group: [HOSTED]
Posts: 975
Joined: 12-April 06
From: Essex, UK
Member No.: 21,719



QUOTE(ghostrider @ Feb 25 2007, 01:57 AM) *
You have it right shadowx. It calls the variable names the names of the elements in the array. I would assume it wouldn't work on numerical arrays, but I'm not sure.


humm this could be useful to me in the case! i always find it so tedious to sit and write 5 lines of assigning variables from a POST array. Thanks for the info!
Go to the top of the page
 
+Quote Post
Blessed
post Mar 27 2007, 11:54 PM
Post #7


Advanced Member
*******

Group: Members
Posts: 144
Joined: 22-March 07
Member No.: 40,472



wow thanx for the tip wink.gif
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Getting List Of Directories And Files Using Php(6)
  2. I Found A Site That Sends Money To Your Paypal(10)
  3. The Sims 2 Nightlife Expansion(10)
  4. Operating System Not Found(13)
  5. Thc Found To Kill Leukemia Cells(12)
  6. How To Use A Link To Call Function In Php?(8)
  7. Include File.php?id=something(13)
  8. Watermark Your Image With Simple Php Script(35)
  9. A Natural Treatment That Works?, Neem Trees(6)
  10. Sherwood A New Mmorpg I Found(20)
  11. Extract Music From Games(20)
  12. Genuine Help With Smtp And Php Mail() Function(3)
  13. How Good Is This Data Cleaning Function?(2)
  14. Does Anyone Here Take Cholestorol Medication?(17)
  15. How I Got Here.(6)
  1. Found Free Mobile Games Wallpapers And Themes(2)
  2. Endif function?(6)
  3. I Am Using A Free Antivirus That I Found On The Web(5)
  4. A:/ Drive Not Found(4)
  5. Sites Down After The Upgrade(8)
  6. I Found Out What Lorem Ipsum Was Yesterday.(8)
  7. Found A New Site(4)
  8. Jack Thompson Found Guilty On 27 Of 31 Misconduct Charges(0)
  9. Question Regarding Php Function(12)
  10. Amazing Software(0)
  11. PHP Function To Add Previous and Next Page Feature(2)
  12. Orkut(1)
  13. How To Extract The Audio From Youtube Videos(2)


 



- Lo-Fi Version Time is now: 5th September 2008 - 11:58 PM