Variable returned in class -- how do i echo it? :S

Reply

Join Date: Aug 2009
Posts: 2
Reputation: EpicDewd is an unknown quantity at this point 
Solved Threads: 0
EpicDewd EpicDewd is offline Offline
Newbie Poster

Variable returned in class -- how do i echo it? :S

 
0
  #1
Sep 13th, 2009
Hi everyone. I have a php mysql class and was wondering how I declare a varialbe kinda thing.

Here is the code in the mysql class....

  1. <?php
  2. // class stuff
  3. public function fetch_array_from_query($query)
  4. {
  5. $this->debug_write("Fetching array from given query");
  6. if(!$this->is_connected())
  7. $this->setup_connection();
  8. $result = $this->query($query);
  9. $this->debug_write("Finished fetching array from given query");
  10. $array = $this->fetch_array($result);
  11. return $array;
  12. }
How would I echo $array out of the class?

I can do echo $array inside the class and it works fine, but if I try to do something like
  1. <?php
  2. global $db;
  3. $db->fetch_array_from_query("Stuff here");
  4. echo $db->$array();
  5. ?>
It doesn't work...

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 77
Reputation: ivatanako is an unknown quantity at this point 
Solved Threads: 5
ivatanako ivatanako is offline Offline
Junior Poster in Training

Re: Variable returned in class -- how do i echo it? :S

 
0
  #2
Sep 13th, 2009
Originally Posted by EpicDewd View Post
Hi everyone. I have a php mysql class and was wondering how I declare a varialbe kinda thing.

Here is the code in the mysql class....

  1. <?php
  2. // class stuff
  3. public function fetch_array_from_query($query)
  4. {
  5. $this->debug_write("Fetching array from given query");
  6. if(!$this->is_connected())
  7. $this->setup_connection();
  8. $result = $this->query($query);
  9. $this->debug_write("Finished fetching array from given query");
  10. $array = $this->fetch_array($result);
  11. return $array;
  12. }
How would I echo $array out of the class?

I can do echo $array inside the class and it works fine, but if I try to do something like
  1. <?php
  2. global $db;
  3. $db->fetch_array_from_query("Stuff here");
  4. echo $db->$array();
  5. ?>
It doesn't work...

Thanks
echo $array[column number or column name]

example:
$array[0]
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 72
Reputation: smartness is an unknown quantity at this point 
Solved Threads: 10
smartness's Avatar
smartness smartness is offline Offline
Junior Poster in Training

Re: Variable returned in class -- how do i echo it? :S

 
0
  #3
Sep 15th, 2009
<?php
global $db;
$array = $db->fetch_array_from_query("Stuff here");

echo "<pre>";
print_r($array);
echo "</pre>";
?>

Try this and you will get a "clear view" of the array, and then simply use
echo $array["FIELD_NAME"] to echo a field!
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 32
Reputation: Schoorsteen is an unknown quantity at this point 
Solved Threads: 0
Schoorsteen Schoorsteen is offline Offline
Light Poster

Re: Variable returned in class -- how do i echo it? :S

 
0
  #4
Sep 15th, 2009
When a variable get's returned it means it is given to the variable.

  1. function sum($a, $b){
  2. return $a+$b;
  3. }
  4.  
  5. echo sum(2, 3); //outputs 5
  6. $answer = sum(3, 1); // puts 4 in $answer
  7. echo $answer; // outputs 4.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC