Wrox Programmer Forums
|
BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6
This is the forum to discuss the Wrox book Beginning PHP, Apache, MySQLWeb Development by Michael K. Glass, Yann Le Scouarnec, Elizabeth Naramore, Gary Mailer, Jeremy Stolz, Jason Gerner; ISBN: 9780764557446
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 11th, 2004, 03:47 AM
Authorized User
 
Join Date: May 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default extract function - Now answered Thank You

Seeing as everyone was so very helpful with my last query on this forum, I thought I'd ask just one more question about the script in the book I don't totally understand.

The following code is in the book:

$query_d = "SELECT people_fullname
        FROM people
        WHERE people_id='$movie_director'";
    $results_d = mysql_query($query_d) or die(mysql_error());
        $row_d = mysql_fetch_array($results_d);
        extract ($row_d);
        $director = $people_fullname

What exactly does extract do? In the book, this function is used for the first time in this script, with absolutely no explanation of waht it does and when to use it.

Thanks once again
 
Old June 11th, 2004, 06:11 AM
Authorized User
 
Join Date: May 2004
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

extract is a "quick and dirty" way to take all of the values in an associative array, and put them into variables with the same name as the keys. For example, let's say you have a database of people, and your query returns the first name, last name, and department. Your data row might look like this:
Code:
$employee = array("first_name" => "George",
                  "last_name"  => "Smith",
                  "dept"       => "Accounting");

If you run extract($employee), you will end up with three variables:
Code:
$first_name = "George"
$last_name = "Smith"
$dept = "Accounting"
Make sense?

If you want more information about this, or in fact any function that you need more information on, check the php manual at www.php.net. In fact, accessing the manual is easy. If you know the function you want to look up, just type in www.php.net/functionname and it will take you right to the page. You'll find help for extract() at http://www.php.net/extract.

Hope that helps!


BuzzLY
aka Michael K. Glass
Author, Beginning PHP, Apache, MySQL Web Development
 
Old June 11th, 2004, 06:15 AM
Registered User
 
Join Date: Jun 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

here it is:
if your table 'mytable' has 2 fields: 'field1' and 'field2'
then you query the table as follows:
$sql = "SELECT field1, field2
        FROM mytable
        WHERE field1='foo'";
    $results = mysql_query($sql) or die(mysql_error());
        $row = mysql_fetch_array($results);

$row now holds 2 elements (4 really but it's off topic)
Array{
    [field1]=> 'foo'
    [field2]=> 'bar'
}


Now if you use extract on your $row it will create 2 variables: $field1 with 'foo' as value and $field2 with 'bar' as value.
extract ($row);

Is it more understandable now? You basically create string variables off an array table.

 
Old June 11th, 2004, 06:44 AM
Authorized User
 
Join Date: May 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks once again guys. All is understood.

I wish the php.net site explained things as well as that, as I looked at php.net/extract but it didn't make it too clear.

I quote from the site: "This function is used to import variables from an array into the current symbol table".

Ok, if you know what "the current symbol table" is.

Anyway, thanks a lot.





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to extract a spreadsheet ravee39 SQL Server 2005 1 April 23rd, 2007 09:20 AM
T-SQl Extract gregalb SQL Server 2000 2 March 14th, 2007 12:59 AM
answered information bar Seeker55 Javascript 1 March 28th, 2006 01:42 AM
Functions - Now Answered Thank you thegooner BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 13 June 10th, 2004 03:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.