Hi guys,
I'm trying to get the hang of PHP OOP and am at a loss on what to do next.
I created two classes
Db.php
Tables.php
The idea is that I send an SQL query to the Db class which connects to the database and returns the result in an array.
I then need to send the array to the Tables class to print out the results in an html table.
Problem is, I can't seem to get any data out of
$data. How would you start writing the Tables class to extract data from the array?
Can this be done?
Here is a sample of what I did:
index.php
Code:
<?php
require_once 'classes/Db.php';
require_once 'classes/Tables.php';
$q1 = new Db;
$data = $q1->ReturnSqlQuery("SELECT User, Host FROM user;"
);
$t1 = new Tables;
$t1->UserHost($data);
Db.php
Code:
<?php
Class Db
{
private $_host = "localhost";
private $_user = "testuser";
private $_pass = "test";
private $_db = "mysql";
// Will need a new class Error to handle all errors
private function ConnectToDatabase()
{
$link = mysql_connect($this->_host, $this->_user, $this->_pass);
if (!$link) {
die('Could not connect to the server: ' . mysql_error());
}
$db_selected = mysql_select_db($this->_db, $link);
if (!$db_selected) {
die ('Could not connect to the database"' . $this->_db ." get error: " . mysql_error());
}
}
private function DisconnectFromDatabase()
{
mysql_close($link);
}
public function ReturnSqlQuery($sql)
{
self::ConnectToDatabase();
$result = mysql_query($sql);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
// return the result to a function that will build the right table.
return $result;
// self::DisconnectFromDatabase();
}
}