PhpRiot

Listing 1766

Submitted by phpro.org, 19 November 2008
<?php
 
    class crud
    {
 
        private $db;
 
        private $error_mode;
 
        /**
         *
         * @Connect to the database
         *
         */
        public function conn()
        {
            $this->db = new PDO("mysql:host=$hostname;port=$db_port;dbname=$dbname", $db_username, $db_password);
            $this->setErrorMode($this->error_mode);
        }
 
 
        /**
         *
         * @set the exception mode
         *
         * @access public
         *
         * @param stirng $error_mode
         *
         */
        public function setErrorMode($error_mode="ERRMODE_EXCEPTION")
        {
            $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::$error_mode);
        }
 
        /**
         * @Run a custom SQL query
         *
         * @access public
         *
         * @param string $sql
         *
         * @throws PDOExcption on failure
         *
         */
        public function dbQuery($sql)
        {
            $stmt = $this->db->prepare($sql);
            $stmt->execute();
        }
 
 
        /***
         *
         * @select values from table
         *
         * @access public
         *
         * @param string $table The name of the table
         *
         * @param string $fieldname
         *
         * @param string $id
         *
         * @return array on success or throw PDOException on failure
         *
         */
        public function dbSelect($table, $fieldname=null, $id=null)
        {
            $sql = "SELECT * FROM `$table`";
            $sql .= is_null($id) ? '' : " WHERE $fieldname=:id";
            $stmt->prepare($sql);
            if(is_null($id))
            {
                $stmt->bindParam($fieldname, $id);
            }
            $stmt->execute();
        }
 
        /**
         *
         * @Insert a value into a table
         *
         * @acces public
         *
         * @param string $table
         *
         * @param array $values
         *
         * @return int The last Insert Id on success or throw PDOexeption on failure
         *
         */
        public function dbInsert($table, $values)
        {
            $sql = "INSERT INTO $table SET ";
            foreach($values as $fieldname=>$value)
            {
                $sql .= "$fieldname = $value ";
            }
            $stmt->prepare($sql);
 
            foreach($values as $fieldname=>$value)
            {
                $stmt->bindParam($fieldname, $value);
            }
            $stmt->execute();
            return $stmt->lastInsertId();
        }
 
        /**
         *
         * @Update a value in a table
         *
         * @access public
         *
         * @param string $table
         *
         * @param string $fieldname, The field to be updated
         *
         * @param string $pk The primary key
         *
         * @param string $id The id
         *
         * @throws PDOException on failure
         *
         */
        public function dbUpdate($table, $fieldname, $pk, $id)
        {
            $sql = "UPDATE $table SET $fieldname = $value WHERE $pk = :id";
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(':id', $id);
            $stmt->execute();
        }
 
 
        /**
         *
         * @Delete a record from a table
         *
         * @access public
         *
         * @param string $table
         *
         * @param string $fieldname
         *
         * @param string $id
         *
         * @throws PDOexception on failure
         *
         */
        public function dbDelete($table, $fieldname, $id)
        {
            $sql = "DELETE FROM `$table` WHERE = $fieldname = :id";
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(':id', $id);
            $stmt->execute();
        }
    }
Submit a Follow Up