Click here to get back home

Naming a php variable using the result of a database query

 HomeNewsGroups | Search

comp.lang.php - PHP programming language discussions 

get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Naming a php variable using the result of a database query kreutz 11-26-2008
Posted by kreutz on November 26, 2008, 5:05 pm
Please log in for more thread options
Newbie post...

My MySQL database table holds the names and values of variables that
I'd like to define and use in php. I can write php by hand, e.g.
$var1 = fdfgdfjg
$var2 = skdfsf
etc.
but I wonder instead if there a way to query the table, obtain the
variable name (which is stored in the table along with the value), and
automatically/quickly create a series of correctly named php variables
(instead of by hand, as above).

Is this possible? Does it even make sense?!

Many thanks in advance, Tom Kreutz

Posted by A.Reader on November 26, 2008, 5:21 pm
Please log in for more thread options
On Wed, 26 Nov 2008 14:05:16 -0800 (PST),

show/hide quoted text

Yes, it's both possible and sensible.

$dset = mysql_query( 'DESCRIBE tablename' ) ;
$temp = array ;
while ( $rec = mysql_fetch_assoc( $dset ) )
{
        $temp[] = $rec ['Name'] ;
// it might not be 'Name', you'll have to look        
}

extract( $temp ) ;

Follow that?

Posted by A.Reader on November 26, 2008, 5:25 pm
Please log in for more thread options
On Wed, 26 Nov 2008 17:21:28 -0500,

show/hide quoted text

I looked: the identifier field is called 'Field', not 'Name'

Posted by kreutz on November 26, 2008, 5:29 pm
Please log in for more thread options
Thanks for your marvelously rapid and detailed reply! It doesn't make
sense to me yet, but I suspect that it will after I dig in and do some
learning. Tom

Posted by A.Reader on November 27, 2008, 5:22 am
Please log in for more thread options
On Wed, 26 Nov 2008 14:29:11 -0800 (PST),

show/hide quoted text

You're welcome. As it turns out, I was in a rush and didn't
remember the process quite correctly. This is correctly:

// DESCRIBE yields a dataset in which each record
// gives all the information about one of the fields in the
// table being described: Field, Type, Null, Key, Default,
// and Extra
$dset = mysql_query( 'DESCRIBE tablename' ) ;

// you'll need a temporary array for extract() to work from
$temp = array() ;

// take each of the fields returned by DESCRIBE
while ( $rec = mysql_fetch_assoc( $dset ) )
{
        // and use the field-name value as the key
        // in the temp array, which is what extract
        // will be looking for.
        $temp[ $rec ['Field'] ]= 'foo' ;
}

// now call extract(), who takes every key in temp
// and turns it into a variable, assigning it whatever
// nominal value ('foo', in this case) you used when
// setting up temp
extract( $temp ) ;

// and you're done.

Similar ThreadsPosted
Assign the result of $db->query($query) to a variable ??? March 1, 2010, 11:06 am
Assign the result of $db->query($query) to a variable ??? March 1, 2010, 11:07 am
Double query form, result of 1st query drops anything after a space October 23, 2004, 4:27 pm
Can you use the result from one query as a criteria in a second query? September 20, 2004, 11:58 am
same mysql query doesn't always return a result September 9, 2005, 9:04 am
displaying the result of a COUNT query in MySQL December 19, 2005, 10:29 am
question about processing MySQL query result June 22, 2006, 1:55 am
Newbie: How to display query result in table? July 6, 2006, 12:36 am
newbie question: linked result from a query August 31, 2006, 8:02 pm
Unable to see full result of mysql query in php October 9, 2007, 11:17 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Driving a better car - Fuelzilla.com

Cabling site for homeowners and pros alike - Cabling-Design.com

Friends:

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap
Privacy Policy