Jump to content


Photo

Retrieving a numericly sequenced echo into a variable


  • Please log in to reply
6 replies to this topic

#1 THUNDER_CHILD

THUNDER_CHILD
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 21 July 2006 - 11:42 PM

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

$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++;
}

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.

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 22 July 2006 - 01:57 AM

$meta = mysql_fetch_field($result, $i);
$your_new_array[$i] = $meta; // add this name to the array
   if (!$meta) {

Legend has it that reading the manual never killed anyone.
My site

#3 THUNDER_CHILD

THUNDER_CHILD
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 22 July 2006 - 02:27 AM

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.

#4 Joe Haley

Joe Haley
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts
  • LocationCanada, eh?

Posted 22 July 2006 - 02:33 AM

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
Give a man a fish; you have fed him for today.  Teach a man to fish; and you have fed him for a lifetime
Don't teach men to program. Teach them to fish.

Please, try the RTFM solution before asking for help:
http://php.net/manual/en/index.php

#5 THUNDER_CHILD

THUNDER_CHILD
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 22 July 2006 - 02:47 AM

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.


#6 THUNDER_CHILD

THUNDER_CHILD
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 22 July 2006 - 03:45 AM

i tried this

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'];
} 

but if i try to echo $column1 it errors out on me.

#7 Joe Haley

Joe Haley
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts
  • LocationCanada, eh?

Posted 22 July 2006 - 04:05 AM

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.
Give a man a fish; you have fed him for today.  Teach a man to fish; and you have fed him for a lifetime
Don't teach men to program. Teach them to fish.

Please, try the RTFM solution before asking for help:
http://php.net/manual/en/index.php




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users