Search Data with REST API in PHP

 <?php


$servername = "localhost";
$username = "root";
$password = "";
$database = "test";

//Create Connection
$conn = new mysqli($servername, $username, $password, $database);

//Check Connection
if ($conn->connect_error) {
    die("Connection Failed: " . $conn->connect_error);
}
Above File is mysqli_conn.php which is included in below files.







Below file is api-search.php File.
<?php

header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");

// $data = json_decode(file_get_contents("php://input"), true);

// $search_value = $data["search"];

//Search through URL as shown below :
$search_value = isset($_GET["search"]) ? $_GET["search"] : die();

include("mysqli_conn.php");

$sql = "SELECT * FROM students WHERE student_name LIKE '%{$search_value}%'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $output = $result->fetch_all(MYSQLI_ASSOC);
    echo json_encode($output);
} else {
    echo json_encode(array(
        "message" => "No Search Found.",
        "status" => false
    ));
}



Comments

Popular posts from this blog

Logical_Operators

SubQuery with EXISTS and NOT EXISTS in PHP

Get Functions