I wonder what are benefits of separating MySQL code from PHP code.
Currently our MySQL code is not separated and we're still evaluating if this is a good thing to do, what are pros / cons, if any or is it the same?
Below is a simple example to help you understand what I mean.
PHP script (log.php):
require_once '../dbconnect.php';
require_once '../classes/database/db_log.php';
$dbActivitylog = new db_tblactivitylog;
$distinct_users = $dbActivitylog->distinct_users();
foreach($distinct_users as $user)
{
echo '<span class="fancy_color">{$user}</span><br />";
}
MySQL code => db_log.php:
class db_tblactivitylog
{
// Return an array of distinct users in the activity log
public function distinct_users()
{
$query = "SELECT DISTINCT user FROM log ORDER BY user ASC";
$result = full_query($query);
$distinct_users = array();
while($data = mysql_fetch_array($result))
{
$distinct_users[] = $data['user'];
}
return $distinct_users;
}
// This function deletes $max_rows_to_delete rows in excess of the activitylimit in config, that match the where condition, keeping the newest rows
public function truncate_rows($where, $max_rows_to_delete=9999)
{
global $CONFIG;
$max_rows_to_keep = $CONFIG['ActivityLimit'];
$result = select_query( "log", "", $where, "id", "DESC", $max_rows_to_keep.",".$max_rows_to_delete);
while($data = mysql_fetch_array($result))
{
// delete query function, nothing unusual
delete_query( "log", array( "id" => $data['id'] ) );
}
}
}
We are planning to use MySQLi, just wonder if there are any direct benefits of separating code as we can do it on the way?