I am really just looking to make sure I am not making any weird mistakes that could hurt me in the long run or if something seems odd in the way I imagine it to work. This code does work for the way I use it and may lack other more advanced things that I may use in the future but have not implemented yet. I left in my phpdoc code to maybe make some sense of weird things. Any feedback is appreciated.
Note: I am constantly reading up on things and try to cover everything in each of the stack sites but I might have overlooked some very basic resources due to be newer to the stack family.
/**
* Class Main Mysql Database
*/
class mydb {
/**
* Connect to DB
*/
public function __construct() {
// Connect to Database
$this->mydb = new mysqli('host', 'database', 'user', 'pass');
if ($this->mydb->connect_errno) { // Error on connection failure
echo "Failed to connect to MySQL in Construct: (" . $this->mydb->connect_errno . ") " . $this->mydb->connect_error;
}
} /** End construct */
/**
* Choose items from the database (SELECT Statement with WHERE equals and like options)
* Designed to be slim and rely more on PHP than mysql for result formating
* @param string $select
* @param string $from
* @param string $config
* @param array $options
* @return array
*/
public function choose ($select, $from, $config = NULL, $options = NULL) {
if ($config === NULL) {
$stmt = "SELECT {$select} FROM {$from}";
} elseif ($config === 'EQUALS') {
$stmt = "SELECT {$select} FROM {$from} WHERE {$options['where_comp']} = '{$options['where_value']}'";
} elseif ($config === 'LIKE') {
$stmt = "SELECT {$select} FROM {$from} WHERE {$options['where_comp']} LIKE '{$options['where_value']}'";
} /** End if $config */
if (!($result = $this->mydb->query($stmt))) {
echo 'Query Error: ' . $result->error . '<br/>';
} else {
while ($row = $result->fetch_assoc()) {
$payload[] = $row;
}
return $payload;
} /** End if mydb->query */
} /** End choose method */
/** Put items into the database (INSERT INTO $table ($col) VALUES ($set)
* This method was designed to do basic inserts without complicating it and filter when $clean is required
* @param string $table
* @param string $col
* @param array $set
* @param string $clean
* */
public function put ($table, $col, $set, $clean = NULL) {
if ($clean) {
$set = "'" . implode("','",filter_var_array($set,FILTER_SANITIZE_STRING)) . "'";
$stmt = "INSERT INTO {$table} ({$col}) VALUES ({$set})";
} else {
$set = "'" . implode("','",$set) . "'";
$stmt = "INSERT INTO {$table} ({$col}) VALUES ({$set})";
} /** End form $stmt */
if (!($putResult = $this->mydb->query($stmt))) {
echo 'Insert Error: ' . $this->mydb->error . '<br/>';
} else {
return $putResult;
}
} /** End put method */
/** Change items in the database (UPDATE $table SET item=value WHERE condition)
* This method was designed to do basic updates without complicating it and filter when $clean is required
* @param string $table
* @param array $set
* @param array $where
* @param string $clean
* */
public function change ($table, $set, $where, $clean = NULL) {
if ($clean) {
foreach ($set as $key => $value) {
$value = filter_var($value, FILTER_SANITIZE_STRING);
$setArray[] = "{$key}='{$value}'";
}
$set = implode(',',$setArray);
$stmt = 'UPDATE ' . $table . ' SET ' . $set . ' WHERE ' . $where['target'] . ' = ' . $where['match'];
} else {
foreach ($set as $key => $value) {
$setArray[] = "{$key}='{$value}'";
}
$set = implode(',',$setArray);
$stmt = 'UPDATE ' . $table . ' SET ' . $set . ' WHERE ' . $where['target'] . ' = ' . $where['match'];
} /** End form $stmt */
if (!($putResult = $this->mydb->query($stmt))) {
echo 'Insert Error: ' . $this->mydb->error . '<br/>';
} else {
return $changeResult;
} /** End Query Check */
} /** End change method */
public function __destruct() {
if (is_object($this->mydb)) {
$this->mydb->close();
}
} /** End destruct */
} /** End mydb Class */
Example of CODE use:
::Include above and create $mydb object::
$set = array('team'=>'First Shift Tech','access'=>'None');
$where = Array('target'=>'author','match'=>'John Doe');
$mydb->change('agent',$set,$where);
Can I pass array's in this line instead? Seems I get an inspection error in phpstorm if I try to do some type of lazy deal.