Jump to content

OOP Database Class and Query Method help


mds1256

Recommended Posts

Hi

 

I have created a Database class that uses the singleton method.

 

I have a query method within this class so i would call this to return an array of results.

 

What i need to know is what would be the best way to use this returned array

 

Database Class:

<?php

class Database
{
private static $dbInstance;
private $hostname;
private $username;
private $password;
private $database;

private function __construct()
{
	$this->hostname = 'localhost';
	$this->username = 'user';
	$this->password = 'pass';
	$this->database = 'test';

	mysql_connect($this->hostname, $this->username, $this->password);
	mysql_select_db($this->database);
}

public static function getDBInstance()
{
	if (!self::$dbInstance) 
    	{ 
     	   self::$dbInstance = new Database(); 
   	 	} 

   	 	return self::$dbInstance; 
}


public function query($q)
{
	return mysql_query($q);
}


}

?>

 

Person Class:

<?php
require_once('Database.php');

class person
{
public function getName($id)
{
	$con = Database::getDBInstance();
	$query = $con->query("select name from data where id = ".$id);
	$result = mysql_fetch_assoc($query);
	echo $result['name'];

}


}

?>

 

Index page:

<?php

require_once('person.php');

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<?php

$person = new person();
$person->getName(1);
echo "<br />";
$person->getName(2);


?>

</body>
</html>

 

Link to comment
Share on other sites

I would seriously consider removing the singleton from this class. Singletons are renowned for making your code tightly coupled which should be avoided where possible.

 

As for your question, I'm not sure I understand the issue. Your "person" model should represent a single row in your database, if you want "persons" you should create another model for that. This "persons" model or collection would then return an array or "person" objects.

Link to comment
Share on other sites

I would seriously consider removing the singleton from this class. Singletons are renowned for making your code tightly coupled which should be avoided where possible.

 

As for your question, I'm not sure I understand the issue. Your "person" model should represent a single row in your database, if you want "persons" you should create another model for that. This "persons" model or collection would then return an array or "person" objects.

 

Hi, thanks for the reply.

 

I am using singleton way so it doesnt create a new database connection / object each time i need to query the database? Is there a better way of achieving this.

 

In the end I would like a database class that has a query method that I can use for each time I need to pull back results.

 

so for example I need the query method to be dynamic and allow a return of an array, I then need to know the best way of allowing me to dynamically handling this array (maybe with another database method).

 

I can do the following but if you look at the index page you will see that I need to store the returned array in a variable first then echo the variable with the array element. Looks a bit messy, is there a way to do this in a one liner?

 

Database class:

<?php

class Database
{
private static $dbInstance;
private $hostname;
private $username;
private $password;
private $database;

private function __construct()
{
	$this->hostname = 'localhost';
	$this->username = 'user';
	$this->password = 'pass';
	$this->database = 'test';

	mysql_connect($this->hostname, $this->username, $this->password);
	mysql_select_db($this->database);
}

public static function getDBInstance()
{
	if (!self::$dbInstance) 
    	{ 
     	   self::$dbInstance = new Database(); 
   	 	} 

   	 	return self::$dbInstance; 
}


public function query($q)
{
	return mysql_query($q);
}


}

?>

 

person class:

<?php
require_once('Database.php');

class person
{
public function getName($id)
{
	$con = Database::getDBInstance();
	$query = $con->query("select name from data where id = ".$id);
	return mysql_fetch_assoc($query);		
}


}

?>

 

index page:

<?php

require_once('person.php');

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<?php

$person = new person();
$name = $person->getName(1);
echo $name['name'];
echo "<br />";
$name = $person->getName(2);
echo $name['name'];


?>

</body>
</html>

Link to comment
Share on other sites

I am using singleton way so it doesnt create a new database connection / object each time i need to query the database? Is there a better way of achieving this.

 

Yes. Make the connection first and pass it into your object upon instantiation.

 

I can do the following but if you look at the index page you will see that I need to store the returned array in a variable first then echo the variable with the array element. Looks a bit messy, is there a way to do this in a one liner?

 

Your querying for two different users so not really. As I said, a "Person" represents a single "person".

Link to comment
Share on other sites

I am using singleton way so it doesnt create a new database connection / object each time i need to query the database? Is there a better way of achieving this.

 

Yes. Make the connection first and pass it into your object upon instantiation.

 

I can do the following but if you look at the index page you will see that I need to store the returned array in a variable first then echo the variable with the array element. Looks a bit messy, is there a way to do this in a one liner?

 

Your querying for two different users so not really. As I said, a "Person" represents a single "person".

 

The latter is correct, I have forgot I am working with objects woops...

 

Do you have example of passing a connection  object into this object.

 

Also can you use array attributes on methods eg.:

 

$person = getName()['firstname']

 

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.