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? Link to comment https://forums.phpfreaks.com/topic/70437-solved-class-method/ 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); Link to comment https://forums.phpfreaks.com/topic/70437-solved-class-method/#findComment-353855 Share on other sites More sharing options...
corillo181 Posted September 24, 2007 Author Share Posted September 24, 2007 it's call a costume class. Link to comment https://forums.phpfreaks.com/topic/70437-solved-class-method/#findComment-353857 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 Link to comment https://forums.phpfreaks.com/topic/70437-solved-class-method/#findComment-353875 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. Link to comment https://forums.phpfreaks.com/topic/70437-solved-class-method/#findComment-353879 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? Link to comment https://forums.phpfreaks.com/topic/70437-solved-class-method/#findComment-353880 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 } } ?> Link to comment https://forums.phpfreaks.com/topic/70437-solved-class-method/#findComment-353884 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. Link to comment https://forums.phpfreaks.com/topic/70437-solved-class-method/#findComment-353889 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. Link to comment https://forums.phpfreaks.com/topic/70437-solved-class-method/#findComment-353894 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.