Hey guys, this being my first real step into OOP (I’ve read tons of stuff on it and I understand most of the concepts, just not when/how I’ll need to use it) so I decided to make a very simple mysql db class, keep in mind it has no security and I usually use PDO, but I did this just for ease of creating the class. Just looking for critique on the actual OOP aspect of it.
class database
{
public $workingTable = NULL;
public $workingQuery = NULL;
public $whereClause = NULL;
function __construct($host, $dbname, $dbuser, $dbpass)
{
$connection = mysql_connect($host, $dbuser, $dbpass) or die("Error connecting to database user.");
$database = mysql_select_db($dbname, $connection) or die("Error connecting to database.");
echo "Connection to $dbname Successful.<br>";
}
function setTable($tableName)
{
$this->workingTable = $tableName;
return $this->workingTable;
}
function where($field, $value)
{
$this->whereClause = "WHERE `$field` = '$value';";
return $this->whereClause;
}
/**
* Default is SELECT * FROM table - so if you have a workingTable already set
* You can perform $database->select(); and it will select everything from your current
* table. This is not considered best practice, so best to individual point out what fields
* you want to select.
* @param string $what
* @param string $tableName
*/
function select($fields="*", $tableName=null)
{
if ($tableName==null)
{
$tableName = $this->workingTable;
} else {
$this->workingTable = $tableName;
}
// So select * is mode 0
// select `this` from is mode 1
// select `this`, `this`, `andthis`, is mode 2
if ($fields=="*")
{
$mode=0;
} else {
if (!is_array($fields))
{
$mode=1;
} else {
$mode=2;
}
}
// Start building Query....
$this->workingQuery = "SELECT ";
switch ($mode)
{
case "0":
$this->workingQuery .= "*";
break;
case "1":
$this->workingQuery .= "`$fields`";
break;
case "2":
foreach ($fields as $field)
{
$this->workingQuery .= "`$field`,";
}
$this->workingQuery = rtrim($this->workingQuery, ",");
break;
}
// add FROM tablename
$this->workingQuery .= " FROM $tableName;";
// Implement the where
if ($this->whereClause != null)
{
$this->workingQuery = str_replace(";", " ", $this->workingQuery);
$this->workingQuery .= $this->whereClause;
}
echo $this->workingQuery;
}
function insert($fields, $values, $tableName=null)
{
if ($tableName==null)
{
$tableName = $this->workingTable;
} else {
$this->workingTable = $tableName;
}
$this->workingQuery = "INSERT INTO `".$this->workingTable."` (";
$mode=0;
if (is_array($fields))
{
$mode=1;
}
switch($mode)
{
case "0": // one field
$this->workingQuery .= "`".$fields."`) VALUES ('$values')";
break;
case "1": // more than one field
// add `field`, `field`, `field`,
foreach ($fields as $field)
{
$this->workingQuery .= "`".$field."`, ";
}
// remove trailing ,
$this->workingQuery = rtrim($this->workingQuery, ", ");
// add ) VALUES (
$this->workingQuery .= ") VALUES (";
foreach ($values as $value)
{
$this->workingQuery .= "'$value', ";
}
// remove trailing ,
$this->workingQuery = rtrim($this->workingQuery, ", ");
// add )
$this->workingQuery .= ");";
break;
}
echo $this->workingQuery;
}
}
All it has is insert/select and a where clause, so if you set a working table using setTable() that will be the table you’re working on until otherwise noted, you can insert single values, or both arrays of fields and values. You can select specific fields but by default it selects *.
My personal critique would be is I’m pretty sure all the variables at the top can be set to protected correct? Since they wouldn’t be used by the end user just the instance of the class itself? And also, maybe I should have a destruct method closing the mysql connection, and setting all the variables back to null? I guess what I’m saying is if I have an instance of this class and setTable to “users” and then I instantiate the class elsewhere, will it carry that value?
Thanks