THUNDER_CHILD Posted July 21, 2006 Share Posted July 21, 2006 Ok so what i am trying to do is extract the text that is echo'd into a variable. this is the code i am working with[code=php:0]$result = mysql_query('select * from disp');if (!$result) { die('Query failed: ' . mysql_error());}/* get column metadata */$i = 0;while ($i < mysql_num_fields($result)) { echo "Column $i:<br />\n"; $meta = mysql_fetch_field($result, $i); if (!$meta) { echo "No information available<br />\n"; } echo "<pre>name: $meta->name</pre>"; $i++;}[/code]What I would like to do is assign each $meta->name to a separate variable that can be used outside of the loop. What the loop does is returns the names of all of the columns within a database and I would like to use them dynamically outside of the loop. Quote Link to comment Share on other sites More sharing options...
AndyB Posted July 22, 2006 Share Posted July 22, 2006 [code]$meta = mysql_fetch_field($result, $i);$your_new_array[$i] = $meta; // add this name to the array if (!$meta) {[/code] Quote Link to comment Share on other sites More sharing options...
THUNDER_CHILD Posted July 22, 2006 Author Share Posted July 22, 2006 Thank You.That helps me get it out of the statement but i am still not sure how to extract the information in the array to individual variables.E.g.i want to take $your_new_array, extract the data in there (a, b, c, d ,e, etc.) and assign each one to a variable $a = a, $b = b, $c = c, etc. Quote Link to comment Share on other sites More sharing options...
Joe Haley Posted July 22, 2006 Share Posted July 22, 2006 if i understand you correctly....${$variable} = $value;is the syntax for variable variables. Example of implimentation:$var1 = 'hi';$var2 = 'bye';${$var1} = $var2;echo $hi; // outputs 'bye', the value of $var2 Quote Link to comment Share on other sites More sharing options...
THUNDER_CHILD Posted July 22, 2006 Author Share Posted July 22, 2006 not exactly. the code that i posted recives the names of the columns of a table, and now thanks to andyb thoes codes now reside in a variable outside of the loop. The variable holds info like column1, column2, column3, etc. I just want to extract thoes bit's of info into their own variable. $column1name = column1, $column2name = column2, $column2name = column2, etc.i hope i made it more clear. Quote Link to comment Share on other sites More sharing options...
THUNDER_CHILD Posted July 22, 2006 Author Share Posted July 22, 2006 i tried this[code=php:0]while($row = mysql_fetch_array( $coulmnlist )) { //where $columnlist is in place of $your_new_aray $column1 = $row['o']; $column2 = $row['1']; $column3 = $row['2']; $column4 = $row['3'];} [/code]but if i try to echo $column1 it errors out on me. Quote Link to comment Share on other sites More sharing options...
Joe Haley Posted July 22, 2006 Share Posted July 22, 2006 mysql_fetch_field() returns an object.$meta is assigned to the returnd value.$columlist[$i] = $meta;mysql_fetch_array() takes a resource, yet you hand it an array of objects.Edit:$coulmnlist[1]->namewould reffer to the name of the returnd object from itteration 1 of the while loop. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.