corillo181 Posted September 24, 2007 Share Posted September 24, 2007 hey I want to know how I can use a fetch_array to do something like this <?php class artistpro{ var $id; var $db; function artistpro($id){ $this->id = $id; $this->db = new DB(); } function body(){ $query = "SELECT * FROM tra_artist WHERE artist_id='{$this->id}'"; $result = $this->db->query($query); $aray = $this->db->fetch_array($result); } function name(){ return $aray['name']; } function age(){ return $aray['age']; } function genre(){ return $aray['genre']; } } ?> the only problem is that this doesn't work with the array, does anyone knows how i could do it? Quote Link to comment Share on other sites More sharing options...
JJohnsenDK Posted September 24, 2007 Share Posted September 24, 2007 why shouldnt this work? looks good to me, only thing i can point at is that you should write mysql infront of fetch_array: $aray = $this->db->mysql_fetch_array($result); Quote Link to comment Share on other sites More sharing options...
corillo181 Posted September 24, 2007 Author Share Posted September 24, 2007 it's call a costume class. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 24, 2007 Share Posted September 24, 2007 Currently, your array variable $aray only exists inside the body() function. Make it a class variable and refer to it as $this->aray Quote Link to comment Share on other sites More sharing options...
corillo181 Posted September 24, 2007 Author Share Posted September 24, 2007 i tried it i add it as a class variable when i try doing var $aray; function body(){ $this->aray = fetch_array() } function name(){ return $this->aray['name'] } the variable comes out empty. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 24, 2007 Share Posted September 24, 2007 Have you called the body() method to get the data? Quote Link to comment Share on other sites More sharing options...
corillo181 Posted September 24, 2007 Author Share Posted September 24, 2007 i tried doing some test using that way and this are the results <?php class artistpro{ var $id; var $db; var $aray; function artistpro($id){ $this->id = $id; $this->db = new DB(); } function body(){ $query = "SELECT * FROM tra_artist WHERE artist_id='{$this->id}'"; $result = $this->db->query($query); $this->aray = $this->db->fetch_array($result); var_dump($this->aray['name']);// = artist name var_dump($this->aray);// all values in array } function name(){ var_dump($this->aray['name']);// = NULL } function age(){ var_dump($this->aray['name']);// = NULL } function genre(){ var_dump($this->aray['genre']);// = NULL } } ?> Quote Link to comment Share on other sites More sharing options...
corillo181 Posted September 24, 2007 Author Share Posted September 24, 2007 AAAH yes Barand i never called the body method to perform the actions.. thank you. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 24, 2007 Share Posted September 24, 2007 I'd call $this->body() from the artistpro() function so you always get the data when you create new object. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.