Query mysql using oop

Hi,
This is my first crack on trying to query with oop
here is what I have


  $user="";
  $host="";
  $password="";
  $database="";
  $connection = mysql_connect($host,$user,$password)
	   or die ("couldn't connect to server" . mysql_error() );
	   
class Mysql 
{
	var $database;
	var $select;
	var $query;
	function Mysql($database)
	{  
		$db = mysql_select_db($database)
		or die ("Couldn't select database");
	}
	function query_select($select)
	{
		$this->query= mysql_query($select);
	}
	function fetch_query()
	{
		return mysql_fetch_array($this->query);
	}
}



  $select = "SELECT * From games";

  $db = new Mysql($database);
  $result = $db->query_select($select);
  
  while ($row = $result->fetch_query())
  {
  echo $row[0] . "1";
  }


tested it out and got this error
Fatal error: Call to a member function on a non-object in …
which points to “return mysql_fetch_array($this->query);”

So far, what I’ve done is not something i’ve found in the book. Some of the example i’ve seen is using the get and set, but what I am trying to do is set up my class with lease amount of code as possible.

Your query-select() method is not returning a result, yet you are trying to assign its return value to something, then use it in an object context.

The fetch_query() method is a member of the mysql class, and you have a reference to an instance of this in $db. That means you’ll want to do

while ($row = $db->fetch_query())
{
// Do stuff here
}

I know you are just trying to get a basic class working, but this isn’t the most sound design. Since the responsibility of the mysql class is probably generally to execute mysql queries, you will probably want a separate class, say MysqlResult, whose responsibility is to retrieve information about that result, or to retrieve rows from it. So, you’d want your query_select() method to return an instance of MysqlResult, which you can get the rows from. Each class should only have one responsibility.

Also, you should probably be connecting to the database from within the mysql class, and passing the connection information within the constructor.

so, what your saying is that I should have one class to select the db, another to mysql_query, and another to just fetch_array?

No. The Mysql class (I’d call it Database) should manage connecting to the db and basically doing anything associated with that db, including executing queries. Then there should be a MysqlResult, or DatabaseResult, class, that allows you to fetch data from the db, get the number of rows, number of rows affected, etc.

I’d refine that slightly and say that the class should encapsulate a single connection to a database server. Php is desperate to make connections “global”. If no connection parameter is specified in one of the mysql_* functions it will just use whatever is lying around. If you reconnect with the same parameters you’ll be given a previous connection made with these parameters, if there is one. You have to take care that connection state - affected rows, current error, etc - is properly encapsulated. Each instance of a Database (or “Connection”) class must have a unique connection. If you want to re-use it you can pass it around where it’s needed.

Would you like to try writing the class using test driven design (TDD)? This turns the normal way you work on its head. Instead of writing code then testing it you write a test first and then just enough code to make it pass.