matthew9090 Posted May 21, 2011 Share Posted May 21, 2011 i was just testing out my oop and wrote this myself <html> <body> <?php error_reporting("E_ALL"); class show { public function connect() { mysql_connect("localhost", "root", ""); } public function sel_db() { mysql_select_db("oop"); } public function get() { $query = mysql_query("SELECT * FROM ppl"); while ($row = mysql_fetch_array($query)) { echo $row['id'] . " | " . $row['name'] . "<br />"; } $num_rows = mysql_num_rows($query); echo $num_rows; } } $a = new show; $a->connect; $a->sel_db; $a->get; ?> </body> </html> but the query doesn't work. I don't get any errors. It will not show anything on the page. It is supposed to show all results from a table. Quote Link to comment https://forums.phpfreaks.com/topic/237033-oop-help/ Share on other sites More sharing options...
Anti-Moronic Posted May 21, 2011 Share Posted May 21, 2011 What you should do is build in validation methods. Like: private function validConnection() { if($this->connection){ return true; } if($this->connection = mysql_connect($this->dbData['host'],$this->dbData['dbuser'],$this->dbData['dbpass'])){ if(mysql_select_db($this->dbData['dbname'],$this->connection)){ return true; } return false; } return false; } Then you use: if($this->validConnection()){ //do queries } ..which should be inside a query method. That way, you know if there are any connection problems, selecting db etc before you even attempt to run any queries. When you run your 'get()' method, you need to check for results before you start iterating over them. As a quick test, add this to your sql functions: mysql_select_db("oop") or die(mysql_error()) Same with mysql_query, and mysql_connect. Don't use this after you know the problem, have these errors stored in an array within your class, and output if you are debugging. Quote Link to comment https://forums.phpfreaks.com/topic/237033-oop-help/#findComment-1218397 Share on other sites More sharing options...
waynew Posted May 21, 2011 Share Posted May 21, 2011 Also, don't encapsulate your PHP classes in HTML. Quote Link to comment https://forums.phpfreaks.com/topic/237033-oop-help/#findComment-1218439 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.