Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Latest commit

 

History

History
103 lines (79 loc) · 1.82 KB

README.md

File metadata and controls

103 lines (79 loc) · 1.82 KB

MySQLiClass

Basic PHP MySQLi class to handle common database queries and operations

Usage

Include and call the class

require_once ('class.php');
$run = new MySQLiDB;

runQuery()

$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";
}

get()

$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>';
}

select()

$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');
}

insert()

$title = $run->sanitize($_POST['title']);
$content = $run->sanitize($_POST['content']);

$data = array(
        'title' => $title,
        'content' => $content,
        );

$insert = $run->insert('blog', $data);

update()

$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);

delete()

$where = array(
      'id' => $id,
      );

$delete = $run->delete('blog', $where);

exists()

$check = array(
      'first_name' => 'John',
      'last_name' => 'Doe'
      );

$column = array('id', 'first_name', 'last_name');

$exists = $run->exists('table', $column, $check);