Jump to content

oop help


matthew9090

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.