<?php
// dbconfig.php
define('DB_WRITER', "root");
define('DB_WRITER_PASSWORD', "root");
define('DB_READER', "root");
define('DB_READER_PASSWORD', "root");
define('DB_DATABASE', "snippets");
define('DB_SERVER', "localhost");
define('DEBUGFLAG', "true");
?>
<?php
// database.php
require "dbconfig.php";
class database {
private $conn;
function __construct($host = DB_SERVER, $dbname = DB_DATABASE,
$user = DB_READER, $password = DB_READER_PASSWORD) {
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->conn = $conn;
}
catch(PDOException $e)
{
if (DEBUGFLAG === "true") {
echo "<div><b>Error: " . $e->getmessage() . "</b></div><br>\n";
} else {
echo "<div><b>Error: database error (1).</b></div><br>\n";
}
return -1;
}
}
public function select_query($sql) {
try {
return $this->conn->query($sql);
}
catch(PDOException $e)
{
if (DEBUGFLAG === "true") {
echo "<div><b>Error: " . $e->getmessage() . "</b></div><br>\n";
} else {
echo "<div><b>Error: database error (2).</b></div><br>\n";
}
return -1;
}
}
public function query_prepared($sql, $values) {
try {
$stmt = $this->conn->prepare($sql);
$stmt->execute($values);
//var_dump($stmt);
return $stmt;
}
catch(PDOException $e)
{
if (DEBUGFLAG === "true") {
echo "<div><b>Error: " . $e->getmessage() . "</b></div><br>\n";
} else {
echo "<div><b>Error: database error (3).</b></div><br>\n";
}
return -1;
}
}
}
?>
// make a list using the database class
require("database.php");
if (!isset($db)) $db = new database();
$sql = "SELECT * FROM category LIMIT 10"; // the first 10 records
$result = $db->select_query($sql);
foreach($result->fetchAll() as $rec) {
// form.php will use the record id for changing or removing the record
echo "<a href='form.php?id=".$rec["id"]."&action=change'>change</a> ";
echo "<a href='form.php?id=".$rec["id"]."&action=remove'>remove</a> ";
echo $rec["id"] . " " . $rec["name"] . "<br>";
}
// search for a record using the database class and a record id
require("database.php");
if (!isset($db)) $db = new database();
$id = 4; // usually set as a url variable in a list (see above)
$sql = "SELECT * FROM category WHERE id = :id";
$values = array(':id'=>$id);
$result = $db->query_prepared($sql,$values);
$rec = $result->fetch(); // maximum one record possible
if (isset($rec)) echo "Record found: " . $rec["id"] . " " . $rec["name"];
// insert a record using the database class
require("database.php");
if (!isset($db)) $db = new database();
$id = null; // must be null when inserting (primary key - auto increment)
$name = "test category";
$active = 0;
$sql = "INSERT INTO category (id, name, active)";
$sql .= " VALUES (:id, :name, :active)";
$values = array (':id' => $id, ':name' => $name, ':active' => $active);
$result = $db->query_prepared($sql, $values);
if ($result->rowCount() > 0) {
echo "Record with name = " . $name . " inserted.<br><br>";
} else {
if ($result->rowCount() == 0) {
echo "No data was changed.<br>";
} else {
echo "Error.<br>";
}
}
echo $sql . "<br>";
var_dump($values);
echo "Number of records affected: " . $result->rowCount();