File: fwphp/01mater/shop/ch24_guitar_shop/model/category_db.php

Recommend this page to a friend!
  Classes of Slavko Srakocic  >  B12 PHP FW  >  fwphp/01mater/shop/ch24_guitar_shop/model/category_db.php  >  Download  
File: fwphp/01mater/shop/ch24_guitar_shop/model/category_db.php
Role: Example script
Content type: text/plain
Description: Example script
Class: B12 PHP FW
Manage database records with a PDO CRUD interface
Author: By
Last change:
Date: 1 month ago
Size: 2,704 bytes
 

Contents

Class file image Download
<?php
function get_categories() {
    global
$db;
   
$query = 'SELECT *,
                (SELECT COUNT(*)
                 FROM products
                 WHERE Products.categoryID = Categories.categoryID)
                 AS productCount
              FROM categories
              ORDER BY categoryID'
;
    try {
       
$statement = $db->prepare($query);
       
$statement->execute();
       
$result = $statement->fetchAll();
       
$statement->closeCursor();
        return
$result;
    } catch (
PDOException $e) {
       
$error_message = $e->getMessage();
       
display_db_error($error_message);
    }
}

function
get_category($category_id) {
    global
$db;
   
$query = '
        SELECT *
        FROM categories
        WHERE categoryID = :category_id'
;
    try {
       
$statement = $db->prepare($query);
       
$statement->bindValue(':category_id', $category_id);
       
$statement->execute();
       
$result = $statement->fetch();
       
$statement->closeCursor();
        return
$result;
    } catch (
PDOException $e) {
       
$error_message = $e->getMessage();
       
display_db_error($error_message);
    }
}

function
add_category($name) {
    global
$db;
   
$query = 'INSERT INTO categories
                 (categoryName)
              VALUES
                 (:name)'
;
    try {
       
$statement = $db->prepare($query);
       
$statement->bindValue(':name', $name);
       
$statement->execute();
       
$statement->closeCursor();

       
// Get the last product ID that was automatically generated
       
$category_id = $db->lastInsertId();
        return
$category_id;
    } catch (
PDOException $e) {
       
$error_message = $e->getMessage();
       
display_db_error($error_message);
    }
}

function
update_category($category_id, $name) {
    global
$db;
   
$query = '
        UPDATE categories
        SET categoryName = :name
        WHERE categoryID = :category_id'
;
    try {
       
$statement = $db->prepare($query);
       
$statement->bindValue(':name', $name);
       
$statement->bindValue(':category_id', $category_id);
       
$statement->execute();
       
$statement->closeCursor();
    } catch (
PDOException $e) {
       
$error_message = $e->getMessage();
       
display_db_error($error_message);
    }
}

function
delete_category($category_id) {
    global
$db;
   
$query = 'DELETE FROM categories WHERE categoryID = :category_id';
    try {
       
$statement = $db->prepare($query);
       
$statement->bindValue(':category_id', $category_id);
       
$statement->execute();
       
$statement->closeCursor();
    } catch (
PDOException $e) {
       
$error_message = $e->getMessage();
       
display_db_error($error_message);
    }
}

?>

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