Dexlin Posted July 20, 2012 Share Posted July 20, 2012 Hi all, I have a class that i have written to get the lastest 3 records from the db and i have got a message saying "Undefined variable $category" in $query. I can not see what i'm doing wrong and its driving me nuts. The class with the problem is as follows: class Get extends DatabaseConnect { public function __Contruct($category) { $this->category = $category; } public function Latest() { if (!@$this->Connect('localhost','root','')) { echo 'Connection Failed'; } else { mysql_select_db ("film"); $query = mysql_query("SELECT * FROM films WHERE category='$category' Limit 3"); $rows = mysql_fetch_array($query); if($query) { while($rows) { $title = $rows['title']; $description = $rows['description']; $body = $rows['body']; $posted = $rows['posted']; } } else { die("Problem"); } } } } Can anyone see what i have done wrong as i can't. Thanks Link to comment https://forums.phpfreaks.com/topic/266005-issue-with-undefined-variable/ Share on other sites More sharing options...
xyph Posted July 20, 2012 Share Posted July 20, 2012 You gotta think about scope here... where in the method Latest have you defined $category? (You haven't) Maybe you meant $this->category ? Link to comment https://forums.phpfreaks.com/topic/266005-issue-with-undefined-variable/#findComment-1363075 Share on other sites More sharing options...
Dexlin Posted July 20, 2012 Author Share Posted July 20, 2012 Hi xyph, I have tried that but this appears "Notice: Undefined property: Get::$category". I have cleared the message by declaring category inside the latest function with $category = ' ';. Thanks Link to comment https://forums.phpfreaks.com/topic/266005-issue-with-undefined-variable/#findComment-1363078 Share on other sites More sharing options...
xyph Posted July 20, 2012 Share Posted July 20, 2012 Ah. Here's your real issue. public function __Contruct($category) { Link to comment https://forums.phpfreaks.com/topic/266005-issue-with-undefined-variable/#findComment-1363080 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.