Commit 69b773a1 authored by danmcquillan's avatar danmcquillan
Browse files

add database class

parent 6a503821
<?
// define constants for database conncetion
define('DATABASE_HOST', 'localhost');
define('DATABASE_NAME', 'recordstore');
define('DATABASE_USER', 'recordstoreuser');
define('DATABASE_PASSWORD', 'recordstorepwd');
?>
<?php
class Database
{
private $connection;
private $hostname;
private $username;
private $password;
private $database;
public function __construct()
{
$this->hostname = DATABASE_HOST;
$this->username = DATABASE_USER;
$this->password = DATABASE_PASSWORD;
$this->database = DATABASE_NAME;
}
public function openConnection()
{
// Open database connection
$this->connection = mysqli_connect($this->hostname, $this->username, $this->password, $this->database)
or die(mysqli_error());
}
public function closeConnection()
{
if (isset($this->connection)) {
// Close database connection
mysql_close($this->connection)
or die(mysql_error());
}
}
public function executeStatement($statement)
{
// Execute database statement
$result = mysqli_query($this->connection, $statement)
or die(mysql_error());
// Return result
return $result;
}
public function executeSql($sql)
{
// Execute database statement
$result = $this->executeStatement($sql);
// Check number of rows returned
if(mysqli_num_rows($result) == 1)
{
// Fetch one row from the result
$dataset = mysqli_fetch_object($result);
}
else
{
// Fetch multiple rows from the result
$dataset = array();
while ($row = mysqli_fetch_object($result)) {
$dataset[] = $row;
}
}
// Close database cursor
mysqli_free_result($result);
// Return dataset
return $dataset;
}
public function executeDml($dml)
{
// Execute database statement
$this->executeStatement($dml);
// Return number of affected rows
return mysqli_affected_rows($this->connection);
}
public function sanitizeInput($value)
{
// more here...
}
}
<?
include 'config.php';
include 'database.class.php';
$db = new Database;
$db->openConnection();
$sql = 'select * from record';
$rows = $db->executeSQL($sql);
foreach($rows as $row) {
print $row->title;
print "<br />";
}
?>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment