Basic PHP MySQLi class to handle common database queries and operations
Include and call the class
require_once ('class.php');
$run = new MySQLiDB;
$id = $run->sanitize($_POST['id']);
$sqlQuery = "SELECT id, first_name, last_name FROM users WHERE id={$id}";
$result = $run->runQuery($sqlQuery);
if ($result->num_rows > 0 ) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["first_name"]. " " . $row["last_name"]. "<br>";
}
} else {
echo "0 result";
}
$cols = array('last_name', 'first_name');
$results = $run->get('users', $cols, 'last_name', 'ASC');
foreach ($results as $result){
echo $result->last_name, ', ',$result->first_name,'<br>';
}
$column = array('id', 'title', 'content', 'dt');
$where = array(
'id' => $id,
);
$result = $run->select($column, 'blog', $where, $limit);
if(!$result->num_rows > 0) {
header('location: index.php');
}
$title = $run->sanitize($_POST['title']);
$content = $run->sanitize($_POST['content']);
$data = array(
'title' => $title,
'content' => $content,
);
$insert = $run->insert('blog', $data);
$title = $run->sanitize($_POST['title']);
$content = $run->sanitize($_POST['content']);
$data = array(
'title' => $title,
'content' => $content,
);
$where = array(
'id' => '22',
);
$update = $run->update('blog', $data, $where);
$where = array(
'id' => $id,
);
$delete = $run->delete('blog', $where);
$check = array(
'first_name' => 'John',
'last_name' => 'Doe'
);
$column = array('id', 'first_name', 'last_name');
$exists = $run->exists('table', $column, $check);