PHP Classes

File: examples/db.example.php

Recommend this page to a friend!
  Classes of Jelle Sebreghts  >  Database class extending PDO  >  examples/db.example.php  >  Download  
File: examples/db.example.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Database class extending PDO
Execute queries composed programatically using PDO
Author: By
Last change: Added license and examples for version 2.
Date: 10 months ago
Size: 1,540 bytes
 

Contents

Class file image Download
<?php

require_once '../vendor/autoload.php';
//change these to your database settings
define("DB_HOST", "localhost");
define("DB_NAME", "db_example");
define("DB_USER", "root");
define("DB_PASSWORD", "root");
$db = new Jelle_S\DataBase\Connection("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASSWORD);

$menu = $db->select('menu')->fields(array('label', 'page'))->orderby('weight')->run()->fetchAllAssoc();

print
"Initial data: <br /><pre>";
print_r($menu);
print
"</pre>";

$fields = array(
 
'label' => 'This is the menu label.',
 
'page' => 'home', //e.g relative link to the page'
 
'weight' => 6,
);
$db->insert('menu')->fields($fields)->run();
$last_insert_id = $db->lastInsertId();

$menu = $db->select('menu')->fields(array('label', 'page'))->orderby('weight')->run()->fetchAllAssoc();

print
"Data after insert: <br /><pre>";
print_r($menu);
print
"</pre>";

$updatefields = array(
 
'label' => 'This is the new label.',
 
'page' => 'contact',
 
'weight' => 5
);

$db->update('menu')->fields($updatefields)->where('id', $last_insert_id)->run();

$menu = $db->select('menu')->fields(array('label', 'page'))->orderby('weight')->run()->fetchAllAssoc();

print
"Data after update: <br /><pre>";
print_r($menu);
print
"</pre>";

$db->delete('menu')->where('id', $last_insert_id)->run();

$menu = $db->select('menu')->fields(array('label', 'page'))->orderby('weight')->run()->fetchAllAssoc();

print
"Data after delete: <br /><pre>";
print_r($menu);
print
"</pre>";


For more information send a message to info at phpclasses dot org.