Jump to content
xisto Community
Sign in to follow this  
ghostrider

The Extract() Function Something I just found out

Recommended Posts

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.

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

Share this post


Link to post
Share on other sites

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

$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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.