Jump to content

Retrieving a numericly sequenced echo into a variable


THUNDER_CHILD

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.
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.
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.
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.
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.

Archived

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

×
×
  • Create New...

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.