php mysql json restful webservice display data based on id
db.php
<?php
$servername="localhost";
$username="root";
$password="";
$database="tg";
//create connection$
$conn=new mysqli($servername, $username, $password, $database);
//check connection
if ($conn->connect_error)
{
die("connection failed: ".$conn->connect_error);
}
?>
$servername="localhost";
$username="root";
$password="";
$database="tg";
//create connection$
$conn=new mysqli($servername, $username, $password, $database);
//check connection
if ($conn->connect_error)
{
die("connection failed: ".$conn->connect_error);
}
?>
Display full list
<?php
include 'includes/db.php';
$cat_query="select * from coachings";
$cat_result=$conn->query($cat_query);
if ($cat_result->num_rows > 0) {
while($cat_row = $cat_result->fetch_assoc()) {
$cat_msg[]=array("name" => $cat_row['c_name'], "address" => $cat_row['c_address'], "city" => $cat_row['c_city'],"state" => $cat_row['c_state'], "country" => $cat_row['c_country'], "pincode" => $cat_row['c_pincode'], "category" => $cat_row['c_category'], "subcategory" => $cat_row['c_subcategory']);
}
} else {
echo "0 results";
}
//$conn->close();
$cat_json = $cat_msg;
header('content-type: application/json');
echo json_encode($cat_json);
@mysqli_close($conn);
?>
include 'includes/db.php';
$cat_query="select * from coachings";
$cat_result=$conn->query($cat_query);
if ($cat_result->num_rows > 0) {
while($cat_row = $cat_result->fetch_assoc()) {
$cat_msg[]=array("name" => $cat_row['c_name'], "address" => $cat_row['c_address'], "city" => $cat_row['c_city'],"state" => $cat_row['c_state'], "country" => $cat_row['c_country'], "pincode" => $cat_row['c_pincode'], "category" => $cat_row['c_category'], "subcategory" => $cat_row['c_subcategory']);
}
} else {
echo "0 results";
}
//$conn->close();
$cat_json = $cat_msg;
header('content-type: application/json');
echo json_encode($cat_json);
@mysqli_close($conn);
?>
display based on id
<?php
include 'includes/db.php';
if (isset($_GET['c_id'])) {
$c_id = $_GET['c_id'];
$cat_query="select * from coachings where c_id=$c_id";
$cat_result=$conn->query($cat_query);
if ($cat_result->num_rows > 0) {
while($cat_row = $cat_result->fetch_assoc()) {
$cat_msg[]=array("name" => $cat_row['c_name'], "address" => $cat_row['c_address'], "city" => $cat_row['c_city'],"state" => $cat_row['c_state'], "country" => $cat_row['c_country'], "pincode" => $cat_row['c_pincode'], "category" => $cat_row['c_category'], "subcategory" => $cat_row['c_subcategory']);
}
} else {
echo "0 results";
}
}
//$conn->close();
$cat_json = $cat_msg;
header('content-type: application/json');
echo json_encode($cat_json);
@mysqli_close($conn);
?>
BASED ON CATEGORY
<?php
include 'includes/db.php';
if (isset($_GET['c_category'])) {
$c_category = $_GET['c_category'];
$cat_query="select * from coachings where c_category='$c_category'";
$cat_result=$conn->query($cat_query);
if ($cat_result->num_rows > 0) {
while($cat_row = $cat_result->fetch_assoc()) {
$cat_msg[]=array("name" => $cat_row['c_name'], "address" => $cat_row['c_address'], "city" => $cat_row['c_city'],"state" => $cat_row['c_state'], "country" => $cat_row['c_country'], "pincode" => $cat_row['c_pincode'], "category" => $cat_row['c_category'], "subcategory" => $cat_row['c_subcategory']);
}
} else {
echo "0 results";
}
}
//$conn->close();
$cat_json = $cat_msg;
header('content-type: application/json');
echo json_encode($cat_json);
@mysqli_close($conn);
?>
Comments
Post a Comment