Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- database info:
- ----------------
- ----------------
- $servername = "localhost";
- $username = "root";
- $password = "";
- $dbname = "webservice";
- table name: items
- no of columns: 4
- columns:
- id int auto increment
- name varchar(100)
- description varchar(100)
- price int(100) // value not more than 1000
- --------------------------
- db_connect.php:
- ---------------
- ----------------
- <?php
- /* Database connection start */
- $servername = "localhost";
- $username = "root";
- $password = "";
- $dbname = "webservice";
- $conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
- if (mysqli_connect_errno()) {
- printf("Connect failed: %s\n", mysqli_connect_error());
- exit();
- }
- ?>
- index.php:
- ------------
- ------------
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>index</title>
- </head>
- <body>
- <title> Demo Create Basic Web Service with PHP, MySQL, XML and JSON</title>
- <div>
- <h2><h2 style="text-align:center">Create Basic Web Service with PHP, MySQL, XML and JSON</h2>
- <br><br>
- <a href="result.php?price=1000&num=10&format=xml" target="_blank"><p style="text-align:center">XML</a> <!-- link as xml -->
- <br><br>
- <a href="result.php?price=1000&num=10&format=json" target="_blank"><p style="text-align:center">JSON</a> <!-- link as json -->
- </div>
- </body>
- </html>
- result.php:
- -----------
- -----------
- <?php
- include_once("db_connect.php");
- if(isset($_GET['price'])) { // in url bar if price is set
- $price = $_GET['price']; // assign price in a var
- $no_of_post = (isset($_GET['num'])?$_GET['num']:10); // condition to hold number of posts
- $format = (isset($_GET['format'])?$_GET['format']:"xml"); // condition to hold format
- $sql_query = "SELECT id, name, description, price FROM items WHERE price <= $price ORDER BY price LIMIT $no_of_post"; // db query
- $resultset = mysqli_query($conn, $sql_query) or die("database error:". mysqli_error($conn)); // connection check
- $products = array();
- if(mysqli_num_rows($resultset) > 0) {
- while($product = mysqli_fetch_assoc($resultset)) {
- $products[] = array('product'=>$product);
- }
- }
- /* output result in required format */
- if($format == 'json') {
- header('Content-type: application/json');
- echo json_encode($products);
- } else if($format == 'xml') {
- header('Content-type: text/xml');
- echo '<products>';
- foreach($products as $index => $product) {
- if(is_array($product)) {
- foreach($product as $key => $value) {
- echo '<',$key,'>';
- if(is_array($value)) {
- foreach($value as $tag => $val) {
- echo '<',$tag,'>',$val,'</',$tag,'>';
- }
- }
- echo '</',$key,'>';
- }
- }
- }
- echo '</products>';
- }
- @mysqli_close($link);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement