Mysql_fetch_field array to a regular PHP array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mrbritesidez
Forum Newbie
Posts: 2
Joined: Wed Jul 15, 2009 12:22 am

Mysql_fetch_field array to a regular PHP array?

Post by mrbritesidez »

Hi everyone,

I know your all busy, so I'd like to thank you in advance and I'll try to keep everything short and concise.

Attempted To:
Convert and/or store the mysql_fetch_field array into a regular PHP array.

Error:
Notice: Undefined property: stdClass::$price

My Thoughts:
I'm new to OOP programming and I'm having a bit of trouble grabbing a set of data out of the database and making them stick inside a PHP array. I don't know whether its the mysql_fetch_field or the way I'm accessing the object. In either case, I'm sure its something silly. Its just i can't seem to get it working. The mysql_fetch_field gets the data and prints it , then it doesn't save it. It discards it. Perhaps its scope? Either way, here is the code. I have tried making it the most legible as possible. Ohhh and the setup function is missing since it connects to the db (has pass and such). Assume it works. (I made sure of it).

Code:

Code: Select all

 
class Billable{
[color=#40FF00]// Private Variable[/color] 
private $con;
private $results;
[color=#40FF00]// resuable ones[/color]
public $row;
public $row2 = array();
  
[color=#40FF00]//Sets the Object Type[/color]
function type($type){
         $query='SELECT * FROM '.$type;
         $this->results=mysql_query($query);
         $row2 = mysql_fetch_field(mysql_query($query));
 
         while (mysql_fetch_array($this->results)){
              echo $row2->price; // [color=#FF0000]This is where the problem is here[/color]
          }
          return ($this->results);
}
 
function getPrice($index){
  [color=#0000FF] //I can't get prices, if I can't get the row2 array[/color]
   return ($index);
}
 
}
echo "<br>Takeup<br>";
 
$TakeUp = new Billable();
$TakeUp->setup(); //Does database connection ... don't worry about it. It works
$TakeUp->type("TakeUp");
echo $TakeUp->getPrice(0);
 
 
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Mysql_fetch_field array to a regular PHP array?

Post by Christopher »

Code: Select all

          while ($row2 = mysql_fetch_assoc($this->results)){
              echo $row2['price']; // [color=#FF0000]This is where the problem is here[/color]
          }
Or;

Code: Select all

         while ($row2 = mysql_fetch_object($this->results)){
              echo $row2->price; // [color=#FF0000]This is where the problem is here[/color]
          }
(#10850)
mrbritesidez
Forum Newbie
Posts: 2
Joined: Wed Jul 15, 2009 12:22 am

Re: Mysql_fetch_field array to a regular PHP array?

Post by mrbritesidez »

Thanks for responding so quickly. I really apprieciate it. But let me try to clarify what i was trying to do. There are two arrays, one array gets the results and other stores them in an easily loopable array. So for example

Code: Select all

row[index] // This store my easily loopable array
row2[id]row2[prices] // This store the mysql fetch array
So what I tried doing was grabbing the the row[index] = row2[price] by using the while loop. It works well within the for loop, but it doesn't work outside of it.

Code: Select all

while ($row2 = mysql_fetch_assoc($this->results)){
               $row[] = $row2['price'];
           echo $row2['price']."<br>"; // This is where the problem is here
           }
Outside the loop when i try calling it in the getPrice() function by using the

Code: Select all

echo $row[$index];
I still get an empty array similar to before.
Notice: Undefined variable: row in /www/test/001.php on line 35
Is there anyway to make it stick?
Post Reply