connection = mysqli_connect( $this->server, $this->username, $this->password, $this->databasename, $this->port ); $this->throwExceptionOnError($this->connection); } public function getAllItems() { $stmt = mysqli_prepare($this->connection, "SELECT CUSTOMER_ID,CUSTOMER_NAME,CUSTOMER_TYPE,CUSTOMER_ADDRESS,ENTRY_MODIFIED_DATE FROM $this->tablename"); $this->throwExceptionOnError(); mysqli_stmt_execute($stmt); $this->throwExceptionOnError(); $rows = array(); mysqli_stmt_bind_result($stmt, $row->customerId,$row->customerName,$row->customerType,$row->customerAddress,$row->entryModifiedDate); while (mysqli_stmt_fetch($stmt)) { $rows[] = $row; $row = new stdClass(); mysqli_stmt_bind_result($stmt, $row->customerId,$row->customerName,$row->customerType,$row->customerAddress,$row->entryModifiedDate); } mysqli_stmt_free_result($stmt); mysqli_close($this->connection); return $rows; } public function getItem($itemID) { $stmt = mysqli_prepare($this->connection, "SELECT CUSTOMER_ID,CUSTOMER_NAME,CUSTOMER_TYPE,CUSTOMER_ADDRESS,ENTRY_MODIFIED_DATE FROM $this->tablename where CUSTOMER_ID = ?"); $this->throwExceptionOnError(); mysqli_bind_param($stmt, 'i', $itemID); $this->throwExceptionOnError(); mysqli_stmt_execute($stmt); $this->throwExceptionOnError(); mysqli_stmt_bind_result($stmt, $row->customerId,$row->customerName,$row->customerType,$row->customerAddress,$row->entryModifiedDate); if(mysqli_stmt_fetch($stmt)) { return $row; } else { return null; } } public function createItem($item) { $stmt = mysqli_prepare($this->connection, "INSERT INTO $this->tablename (CUSTOMER_NAME,CUSTOMER_TYPE,CUSTOMER_ADDRESS,ENTRY_MODIFIED_DATE) VALUES (?, ?,?, NOW())"); $this->throwExceptionOnError(); mysqli_bind_param($stmt, 'sss', $item->customerName,$item->customerType,$item->customerAddress); $this->throwExceptionOnError(); mysqli_stmt_execute($stmt); $this->throwExceptionOnError(); $autoid = mysqli_stmt_insert_id($stmt); mysqli_stmt_free_result($stmt); mysqli_close($this->connection); return $autoid; } public function updateItem($item) { $stmt = mysqli_prepare($this->connection, "UPDATE $this->tablename SET CUSTOMER_NAME=?, CUSTOMER_TYPE=?, CUSTOMER_ADDRESS=?, ENTRY_MODIFIED_DATE = NOW() WHERE CUSTOMER_ID=?"); $this->throwExceptionOnError(); // // Refer documentation for 'types' parameter at http://www.php.net/manual/en/mysqli-stmt.bind-param.php // mysqli_bind_param($stmt, 'sssi', $item->customerName,$item->customerType,$item->customerAddress,$item->customerId); $this->throwExceptionOnError(); mysqli_stmt_execute($stmt); $this->throwExceptionOnError(); mysqli_stmt_free_result($stmt); mysqli_close($this->connection); } public function deleteItem($itemID) { $stmt = mysqli_prepare($this->connection, "DELETE FROM $this->tablename WHERE CUSTOMER_ID = ?"); $this->throwExceptionOnError(); mysqli_bind_param($stmt, 'i', $itemID); mysqli_stmt_execute($stmt); $this->throwExceptionOnError(); mysqli_stmt_free_result($stmt); mysqli_close($this->connection); } public function count() { $stmt = mysqli_prepare($this->connection, "SELECT COUNT(*) AS COUNT FROM $this->tablename"); $this->throwExceptionOnError(); mysqli_stmt_execute($stmt); $this->throwExceptionOnError(); mysqli_stmt_bind_result($stmt, $rec_count); $this->throwExceptionOnError(); mysqli_stmt_fetch($stmt); $this->throwExceptionOnError(); mysqli_stmt_free_result($stmt); mysqli_close($this->connection); return $rec_count; } public function getItems_paged($startIndex, $numItems) { $stmt = mysqli_prepare($this->connection, "SELECT CUSTOMER_ID,CUSTOMER_NAME,CUSTOMER_TYPE,CUSTOMER_ADDRESS,ENTRY_MODIFIED_DATE FROM $this->tablename LIMIT ?, ?"); $this->throwExceptionOnError(); mysqli_bind_param($stmt, 'ii', $startIndex, $numItems); mysqli_stmt_execute($stmt); $this->throwExceptionOnError(); $rows = array(); mysqli_stmt_bind_result($stmt, $row->customerId,$row->customerName,$row->customerType,$row->customerAddress,$row->entryModifiedDate); while (mysqli_stmt_fetch($stmt)) { $rows[] = $row; $row = new stdClass(); mysqli_stmt_bind_result($stmt, $row->customerId,$row->customerName,$row->customerType,$row->customerAddress,$row->entryModifiedDate); } mysqli_stmt_free_result($stmt); mysqli_close($this->connection); return $rows; } /** * Utitity function to throw an exception if an error occurs * while running a mysql command. */ private function throwExceptionOnError($link = null) { if($link == null) { $link = $this->connection; } if(mysqli_error($link)) { $msg = mysqli_errno($link) . ": " . mysqli_error($link); throw new Exception('MySQL Error - '. $msg); } } } ?>