Jump to content

Archived

This topic is now archived and is closed to further replies.

THUNDER_CHILD

Retrieving a numericly sequenced echo into a variable

Recommended Posts

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.

Share this post


Link to post
Share on other sites
[code]$meta = mysql_fetch_field($result, $i);
$your_new_array[$i] = $meta; // add this name to the array
  if (!$meta) {[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]->name
would reffer to the name of the returnd object from itteration 1 of the while loop.

Share this post


Link to post
Share on other sites

×

Important Information

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.