Tuesday, July 19, 2016

REST Api

<?php 
$con=new mysqli('localhost','root','','items');

if(!$con){
echo "not succeeded";
}

if(!empty($_GET['id'])){
$id=$_GET['id'];
$sql = "SELECT * FROM products where id='$id'";
       $result = $con->query($sql);


if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
    header('Content-Type: application/json');
       echo json_encode($row);
    }
     
} else {
    echo "0 results";
}

$con->close();
}
?>


CREATE TABLE IF NOT EXISTS `products` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  `desc` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `products`
--

INSERT INTO `products` (`id`, `name`, `desc`) VALUES
(1, 'books', 'imported'),

(2, 'tables', 'sold');