0

Currently, my website runs procedural PHP. I would like to achieve to have a database class that can be used for other classes.

STATUS QUO:

On every page I include my dp.php before my header.php, the content and footer.php appear. My db.php looks like this:

// Credentials
$dbhost = "localhost";
$dbname = "dbname";
$dbuser = "dbuser";
$dbpass = "dbpassword";

//  Connection
global $db;

$db = new mysqli();
$db->connect($dbhost, $dbuser, $dbpass, $dbname);
$db->set_charset("utf8");

//  Check Connection
if ($db->connect_errno) {
    printf("Connect failed: %s\n", $db->connect_error);
exit();
}

GOAL:

Having a database object that I can integrate into the EventData class that I have been building. The desired function of this class is that it will allow me to easily access data of my events table on any page. My EventData-class.php looks like this:

Class EventData {

private $_db;
private $_event_id;

public function __construct($eventID) {
    $this -> _event_id = $eventID;
}

public function getValue($fieldname) {

    // Build query for getting event details
    $query = 'SELECT * FROM events WHERE id=' . $this -> _event_id . '';

    // Do Search
    $results = $db->query($query);

    // Store all event details available in variables
    while ($result = $results->fetch_assoc()) {
        $value = $result[$fieldname];
    }

    // Return value
    return $value;

}

}

I am new to OOP and am interested in learning how I need to modify both db.php and EventData-class.php in order to work.

Kent Miller
  • 499
  • 2
  • 8
  • 21
  • Have you read the manual (http://php.net/manual/en/mysqli.quickstart.dual-interface.php)? In my opinion it would be better to write a wrapper class for this, so it fits your table structure. – Voitcus Jul 03 '14 at 19:27
  • This is a very broad topic. You can learn alot and get inspiration from looking into popular framework's solutions – kero Jul 03 '14 at 19:28
  • You might find this useful: http://stackoverflow.com/a/11369679/727208 ... of course you will have to replace PDO with MySQLi, but the approach is the same – tereško Jul 03 '14 at 19:30
  • @voitcus: I checked the link Voitcus provided. So do I see it right that I can create a Database-class.php and put inside the following code? $mysqli = new mysqli("example.com", "user", "password", "database"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: " . $mysqli->connect_error; } – Kent Miller Jul 03 '14 at 19:34
  • I'm not exactly sure what you want to achieve, but if your idea is to share '$db' with the EventData class I would create a class being a collection of EventData items and within the collection there would be a field $db which you inject into the collection with its constructor. Each EventData should have a $collection property to allow you access to the $db eg. $item->collection->db – Voitcus Jul 03 '14 at 19:41
  • @Voitcus okay, you have convinced me. I would like to keep the db.php I have at the moment. Having now a look at my EventData-class.php: How can I integrate my database connection? What would be the best way? Do you have a concerete code you can share? – Kent Miller Jul 03 '14 at 19:52
  • I've put something but only with an assumption your code works (I haven't tested it) – Voitcus Jul 03 '14 at 20:11

1 Answers1

0

The answer is brief and needs some your effort to improve or encode the idea.

First of all create a collection of items, something like this

class EventCollection {
  protected $items = array(); // I keep protected so only addItem, removeItem etc. can be used to handle data
  public $database = NULL;

  public function addItem(EventData $item){
    $this->items[] = $item;
    $item->collection = $this; // this allows your EventData object to refer to its container
  }
  public function clear(){
    $this->items = array();
  }

  public function __construct(mysqli $datb){ // inject mysqli object
    $this->database = $datb;
  }

  public function removeItem... etc.
}

Having prepared the collection class, just update your EventData (consider creating a child class) by $collection field:

class EventData {

  private $_db; // throw this away, not necessary
  private $_event_id;
  public $collection = NULL;
  // etc.

Then in your main code create a $db variable as you do. Then create a (empty) collection:

 $myEventCollection = new EventCollection($db);

and add items by

 $myEvent = ....;
 $myEventCollection->add($myEvent);

To refer to $db value inside the EventData class use something like this

 class EventClass { ....
 public function updateEvent(){
   $this->collection->database->query('UPDATE ...');
Voitcus
  • 4,463
  • 4
  • 24
  • 40
  • @KentMiller Your `$db` variable is object and it seems to be ok. The only problem is that one should avoid `global` keyword. Using a collection you can keep the common database object for all its items. You might consider that this collection is some sort of a database table. – Voitcus Jul 03 '14 at 20:19
  • @KentMiller: I'm not sure I'd endorse growing your own. Aside from mature PHP ORM systems (Doctrine, Propel) there are loads of PDO/MySQLi wrapper classes on Github (and elsewhere). There will be something out there that is a good fit for you, will save time, and will be well-tested already. – halfer Jul 03 '14 at 20:27