I am a fan of loose coupling so I would suggest that you take the values of host, username, password and database out of your class. Pass the values through the constructor and set them in the constructor. This makes it easier to reuse your code.
Second, in classes such as one that creates a connection to a database I would not "echo" stuff. You better throw exceptions on error or return meaningful values to the caller.
Last, I'd suggest other names. This might be nitpicking but this has more meaning to me:
$connection = new DatabaseConnection();
$connection->open();
$connection->connectDatabase();
So, wrapped up I would have something like:
<?php
class DatabaseConnection
{
var $host;
var $username;
var $password;
var $database;
var $myconn;
function __construct($dbhost, $user, $pass, $db)
{
$this->host = $dbhost;
$this->username = $user;
$this->password = $pass;
$this->database = $db;
}
function open()
{
$conn = mysql_connect($his->host,$this->username,$this->password);
if (!$conn)
throw new Exception('Error establishing a connection.');
else
$this->myconn = $conn;
return $this->myconn;
}
function connectDatabase()
{
mysql_select_db($this->database);
if(mysql_error())
throw new Exception("Cannot find the database " . $this->database);
}
function selectData($query)
{
mysql_select_db($this->database);
$result = mysql_query($query);
return $result;
}
function closeConnection()
{
mysql_close($this->myconn);
}
}
?>
Usage:
$connection = new DatabaseConnection("localhost", "root", "", "wilbert");
$connection->open();
$connection->connectDatabase();
$connection->selectData("SELECT a.student_id, a.FirstName, b.First_Name FROM Student a LEFT JOIN teacher b ON a.student_id = b.student_id");
See how the other names make more sense? Try to avoid verbs as a classname. Methods can and most likely will be or contain verbs, but not classnames. Also, leaving the query out of the class leaves you the option to use the same code for other queries.
Note that this is no perfect code, I was only suggesting what might be changed. Hope this helps! :)
Edit:
You call the function selectData() and pass the query-string as a parameter. This changes nothing, it gives the same result as your code. Only in my case you can pass other queries to the function whereas your function is limited to the embedded query.
Now for the result. This has also to do with the decoupling of your connection-class and what will be shown to the user. I showed u the usage of the class, the code below is how you can process the result. Create a function (but not in the connection class!) that returns the result in a table:
function resultTable($sqlResult)
{
$table = "<table border='1'>";
$table .= "<tr><th>Student_ID</th><th>First_Name</th><th>First_Name</th></tr>";
while($row = mysql_fetch_array($sqlResult))
{
$table .= "<tr><td>" . $row['student_id']. "</td>";
$table .= "<td>" . $row['FirstName'] . "</td>";
$table .= "<td>" . $row['First_Name'] . "</td></tr>";
}
$table .= "</table>";
return $table;
}
And the usage:
$connection = new DatabaseConnection("localhost", "root", "", "wilbert");
$connection->open();
$connection->connectDatabase();
$result = $connection->selectData("SELECT a.student_id, a.FirstName, b.First_Name FROM Student a LEFT JOIN teacher b ON a.student_id = b.student_id");
echo resultTable($result);
You could create a new class in which you define all sorts of methods like these to process a result and return it to the view-file where you will show it on the screen. Hope this clarifies it. :)
mysql_*
) too. – BenM Feb 27 at 18:20mysql_connect($his->host,$t...
) – symcbean Mar 5 at 21:09$this
– lexter Mar 7 at 3:45