connection = mysqli_connect("servername", "username", "password", "databasename") or die(mysqli_connect_error()); } public function getAllItems() { // TODO Auto-generated method stub // Retrieve a array of records from the database and return that // Sample code $this->connect(); $sql = "SELECT * FROM customers"; $result = mysqli_query($this->connection, $sql) or die('Query failed: ' . mysqli_error($this->connection)); $rows = array(); while ($row = mysqli_fetch_object($result)) { $rows[] = $row; } mysqli_free_result($result); mysqli_close($this->connection); return $rows; } public function getItem($itemID) { // TODO Auto-generated method stub // Return a single record from the database and return the item // Sample code $this->connect(); $itemID = mysqli_real_escape_string($this->connection, $itemID); $sql = "SELECT * FROM customers where customer_id=$itemID"; $result = mysqli_query($this->connection, $sql) or die('Query failed: ' . mysqli_error($this->connection)); $rows = array(); while ($row = mysqli_fetch_object($result)) { $rows[] = $row; } mysqli_free_result($result); mysqli_close($this->connection); return $rows; } public function createItem($item) { // TODO Auto-generated method stub // Insert a new record in the database using the parameter and return the item // Sample code $this->connect(); $sql = "INSERT INTO customers (customer_name, customer_address, customer_type) VALUES ('$item->customer_name','$item->customer_address','$item->customer_type')"; mysqli_query($this->connection, $sql) or die('Query failed: ' . mysqli_error($this->connection)); $autoid= mysqli_insert_id($this->connection); mysqli_close($this->connection); return $autoid; } public function updateItem($item) { // TODO Auto-generated method stub // Update an existing record in the database and return the item // Sample code $this->connect(); $sql = "UPDATE customers SET customer_name = '$item->customer_name', customer_address = '$item->customer_address', customer_type = '$item->customer_type' WHERE customer_id = $item->customer_id"; mysqli_query($this->connection, $sql) or die('Query failed: ' . mysqli_error($this->connection)); mysqli_close($this->connection); } public function deleteItem($itemID) { // TODO Auto-generated method stub // Delete a record in the database // Sample code $this->connect(); $itemID = mysqli_real_escape_string($this->connection, $itemID); $sql = "DELETE FROM customers WHERE customer_id = $itemID"; mysqli_query($this->connection, $sql) or die('Query failed: ' . mysqli_error($this->connection)); mysqli_close($this->connection); } public function count() { // TODO Auto-generated method stub // Return the number of items in your array of records // Sample code $this->connect(); $sql = "SELECT * FROM customers"; $result = mysqli_query($this->connection, $sql) or die('Query failed: ' . mysqli_error($this->connection)); $rec_count = mysqli_num_rows($result); mysqli_free_result($result); mysqli_close($this->connection); return $rec_count; } public function getItems_paged($startIndex, $numItems) { // TODO Auto-generated method stub // Return a page of records as an array from the database for this startIndex // Sample code $this->connect(); $startIndex = mysqli_real_escape_string($this->connection, $startIndex); $numItems = mysqli_real_escape_string($this->connection, $numItems); $sql = "SELECT * FROM customers LIMIT $startIndex, $numItems"; $result = mysqli_query($this->connection, $sql) or die('Query failed: ' . mysqli_error($this->connection)); $rows = array(); while ($row = mysqli_fetch_object($result)) { $rows[] = $row; } mysqli_free_result($result); mysqli_close($this->connection); return $rows; } } ?>