943,783 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 915
  • PHP RSS
Sep 13th, 2009
0

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

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

PHP Syntax (Toggle Plain Text)
  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
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. global $db;
  3. $db->fetch_array_from_query("Stuff here");
  4. echo $db->$array();
  5. ?>
It doesn't work...

Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
EpicDewd is offline Offline
3 posts
since Aug 2009
Sep 13th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by EpicDewd ...
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....

PHP Syntax (Toggle Plain Text)
  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
PHP Syntax (Toggle Plain Text)
  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]
Reputation Points: 18
Solved Threads: 15
Junior Poster
ivatanako is offline Offline
150 posts
since Jul 2007
Sep 15th, 2009
0

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

<?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!
Reputation Points: 7
Solved Threads: 14
Junior Poster
smartness is offline Offline
103 posts
since Aug 2007
Sep 15th, 2009
0

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

When a variable get's returned it means it is given to the variable.

PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 3
Light Poster
Schoorsteen is offline Offline
44 posts
since Aug 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Start php! Adding Php interpreter to Aphach
Next Thread in PHP Forum Timeline: Mail function Error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC