Hướng dẫn how can i create api in php and mysql database? - làm cách nào để tạo api trong php và cơ sở dữ liệu mysql?

Show

Tìm hiểu cách tạo dịch vụ web yên tĩnh với PHP & MySQL trong vòng chưa đầy năm phút

Tôi đang viết hướng dẫn này để chỉ cho bạn cách tạo một dịch vụ web yên tĩnh với PHP 7 & MySQL 8 trong một thời gian rất ngắn. Bạn có thể nghĩ về bài viết này như một mồi. Tôi sẽ cố gắng giải thích các chủ đề cốt lõi càng nhiều càng tốt trong khi tránh những điều chung chung.RESTful web-service with PHP 7 & MySQL 8 in a very short time. You can think of this post as a primer. I’ll try to explain the core topics as much as possible while avoiding generic things.

Xin lưu ý rằng tiện ích mở rộng mysql cho PHP đã bị ngừng hoàn toàn trong PHP 7. Đó là lý do tại sao bạn chỉ có thể sử dụng tiện ích mở rộng mysqli. Toàn bộ API của tôi chỉ dựa trên tiện ích mở rộng mysqli.

Hướng dẫn này giả định rằng bạn biết: -

  • API & REST có nghĩa là gì.
  • Máy khách nghỉ ngơi là gì.
  • Dịch vụ RESTFUL là gì.
  • Làm thế nào để thiết lập một máy chủ web. Tôi đang sử dụng

    $config = parse_ini_file('/Users/admin/Sites/to-do.ini');
    $conn = mysqli_connect($config['dbhost'], $config['username'], $config['password']);
    mysqli_select_db($conn, $config['db']);
    1.
  • Cách cài đặt các mô -đun Apache cần thiết.

Mô tả ứng dụng

Đây là một ứng dụng rất đơn giản hoàn toàn dựa trên kiến ​​trúc REST & không có GUI nào. Bạn tạo hoặc truy xuất việc cần làm chỉ bằng API REST.To-Do app that is completely based on REST architecture & doesn’t have any GUI. You create or retrieve your To-Do’s using the REST API only.

Việc cần làm mới được tạo trong cơ sở dữ liệu MySQL.

Tôi đã giữ API này cực kỳ đơn giản về mục đích. Mục đích duy nhất của tôi ở đây là để bạn (& tôi) bắt đầu với việc viết API và tương tác với nó. Tôi đã bỏ qua bảo mật cơ sở dữ liệu cố ý và sẽ sửa đổi mã sau. Hướng dẫn này là tất cả về việc biết API được tạo như thế nào và làm thế nào bạn có thể thấy phần còn lại trong hành động.

Yêu cầu

  • Php 7
  • MySQL 8
  • Khách hàng nghỉ ngơi như Postman.

    $config = parse_ini_file('/Users/admin/Sites/to-do.ini');
    $conn = mysqli_connect($config['dbhost'], $config['username'], $config['password']);
    mysqli_select_db($conn, $config['db']);
    2 hoặc trình duyệt yêu thích của bạn cũng có thể được sử dụng.

Tôi đang sử dụng


$config = parse_ini_file('/Users/admin/Sites/to-do.ini');
$conn = mysqli_connect($config['dbhost'], $config['username'], $config['password']);
mysqli_select_db($conn, $config['db']);
3,

$config = parse_ini_file('/Users/admin/Sites/to-do.ini');
$conn = mysqli_connect($config['dbhost'], $config['username'], $config['password']);
mysqli_select_db($conn, $config['db']);
4 & Postman cho hướng dẫn này. Bạn có thể sử dụng bất kỳ ứng dụng khách nào mà bạn chọn. Tôi thích Postman hơn vì nó làm cho nó rất dễ dàng và thuận tiện để làm việc với phần còn lại.

Các bước để thực hiện

1. Tạo cơ sở dữ liệu & bảng

Tạo một cơ sở dữ liệu & bảng trong MySQL để lưu trữ dữ liệu.

CREATE DATABASE IF NOT EXISTS `my_to_do_db`;
USE my_to_do_db
--
-- Table structure for table `my_to_do_tb`
--
CREATE TABLE IF NOT EXISTS `my_to_do_tb` (
`task` text NOT NULL,
`date` text NOT NULL,
`priority` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;

2. Tạo tập lệnh

Tập lệnh PHP này sẽ lưu trữ thông tin liên quan đến kết nối cơ sở dữ liệu. Tôi đã giữ thông tin đăng nhập cơ sở dữ liệu trong một tệp riêng


$config = parse_ini_file('/Users/admin/Sites/to-do.ini');
$conn = mysqli_connect($config['dbhost'], $config['username'], $config['password']);
mysqli_select_db($conn, $config['db']);
6 để tránh mã hóa cứng mật khẩu. Tệp này nằm trong một thư mục một cấp trên

$config = parse_ini_file('/Users/admin/Sites/to-do.ini');
$conn = mysqli_connect($config['dbhost'], $config['username'], $config['password']);
mysqli_select_db($conn, $config['db']);
7. Bạn có thể đặt tập tin này ở bất cứ đâu. Chỉ cần đảm bảo rằng bạn tham khảo vị trí chính xác trong tập lệnh

$config = parse_ini_file('/Users/admin/Sites/to-do.ini');
$conn = mysqli_connect($config['dbhost'], $config['username'], $config['password']);
mysqli_select_db($conn, $config['db']);
5.


$config = parse_ini_file('/Users/admin/Sites/to-do.ini');
$conn = mysqli_connect($config['dbhost'], $config['username'], $config['password']);
mysqli_select_db($conn, $config['db']);
5 Tài liệu tham khảo tập lệnh

$config = parse_ini_file('/Users/admin/Sites/to-do.ini');
$conn = mysqli_connect($config['dbhost'], $config['username'], $config['password']);
mysqli_select_db($conn, $config['db']);
6 Tệp.


$config = parse_ini_file('/Users/admin/Sites/to-do.ini');
$conn = mysqli_connect($config['dbhost'], $config['username'], $config['password']);
mysqli_select_db($conn, $config['db']);

Vui lòng kiểm tra kho lưu trữ GitHub của tôi (được liên kết thêm bên dưới) để biết thêm thông tin về tệp này và vị trí của nó.

3. Tạo tập lệnh PHP 1, "Success" => "To-Do has been added successfully!"); } else{ $json = array("status" => 0, "Error" => "Error adding To-Do! Please try again!"); }}else{ $json = array("status" => 0, "Info" => "Request method not accepted!");}@mysqli_close($conn);// Set Content-type to JSONheader('Content-type: application/json');echo json_encode($json);1 để thêm

Bây giờ hãy viết một tập lệnh PHP sẽ đặt dữ liệu vào bảng MySQL. Ý tưởng ở đây là lấy ba giá trị viz


include_once('config.php');
if($_SERVER['REQUEST_METHOD'] == "POST"){
// Get data from the REST client
$task = isset($_POST['task']) ? mysqli_real_escape_string($conn, $_POST['task']) : "";
$date = isset($_POST['date']) ? mysqli_real_escape_string($conn, $_POST['date']) : "";
$priority = isset($_POST['priority']) ? mysqli_real_escape_string($conn, $_POST['priority']) : "";
// Insert data into database
$sql = "INSERT INTO `my_to_do_db`.`my_to_do_tb` (`task`, `date`, `priority`) VALUES ('$task', '$date', '$priority');";
$post_data_query = mysqli_query($conn, $sql);
if($post_data_query){
$json = array("status" => 1, "Success" => "To-Do has been added successfully!");
}
else{
$json = array("status" => 0, "Error" => "Error adding To-Do! Please try again!");
}
}
else{
$json = array("status" => 0, "Info" => "Request method not accepted!");
}
@mysqli_close($conn);
// Set Content-type to JSON
header('Content-type: application/json');
echo json_encode($json);
2,

include_once('config.php');
if($_SERVER['REQUEST_METHOD'] == "POST"){
// Get data from the REST client
$task = isset($_POST['task']) ? mysqli_real_escape_string($conn, $_POST['task']) : "";
$date = isset($_POST['date']) ? mysqli_real_escape_string($conn, $_POST['date']) : "";
$priority = isset($_POST['priority']) ? mysqli_real_escape_string($conn, $_POST['priority']) : "";
// Insert data into database
$sql = "INSERT INTO `my_to_do_db`.`my_to_do_tb` (`task`, `date`, `priority`) VALUES ('$task', '$date', '$priority');";
$post_data_query = mysqli_query($conn, $sql);
if($post_data_query){
$json = array("status" => 1, "Success" => "To-Do has been added successfully!");
}
else{
$json = array("status" => 0, "Error" => "Error adding To-Do! Please try again!");
}
}
else{
$json = array("status" => 0, "Info" => "Request method not accepted!");
}
@mysqli_close($conn);
// Set Content-type to JSON
header('Content-type: application/json');
echo json_encode($json);
3 &

include_once('config.php');
if($_SERVER['REQUEST_METHOD'] == "POST"){
// Get data from the REST client
$task = isset($_POST['task']) ? mysqli_real_escape_string($conn, $_POST['task']) : "";
$date = isset($_POST['date']) ? mysqli_real_escape_string($conn, $_POST['date']) : "";
$priority = isset($_POST['priority']) ? mysqli_real_escape_string($conn, $_POST['priority']) : "";
// Insert data into database
$sql = "INSERT INTO `my_to_do_db`.`my_to_do_tb` (`task`, `date`, `priority`) VALUES ('$task', '$date', '$priority');";
$post_data_query = mysqli_query($conn, $sql);
if($post_data_query){
$json = array("status" => 1, "Success" => "To-Do has been added successfully!");
}
else{
$json = array("status" => 0, "Error" => "Error adding To-Do! Please try again!");
}
}
else{
$json = array("status" => 0, "Info" => "Request method not accepted!");
}
@mysqli_close($conn);
// Set Content-type to JSON
header('Content-type: application/json');
echo json_encode($json);
4 dưới dạng tải trọng &

include_once('config.php');
if($_SERVER['REQUEST_METHOD'] == "POST"){
// Get data from the REST client
$task = isset($_POST['task']) ? mysqli_real_escape_string($conn, $_POST['task']) : "";
$date = isset($_POST['date']) ? mysqli_real_escape_string($conn, $_POST['date']) : "";
$priority = isset($_POST['priority']) ? mysqli_real_escape_string($conn, $_POST['priority']) : "";
// Insert data into database
$sql = "INSERT INTO `my_to_do_db`.`my_to_do_tb` (`task`, `date`, `priority`) VALUES ('$task', '$date', '$priority');";
$post_data_query = mysqli_query($conn, $sql);
if($post_data_query){
$json = array("status" => 1, "Success" => "To-Do has been added successfully!");
}
else{
$json = array("status" => 0, "Error" => "Error adding To-Do! Please try again!");
}
}
else{
$json = array("status" => 0, "Info" => "Request method not accepted!");
}
@mysqli_close($conn);
// Set Content-type to JSON
header('Content-type: application/json');
echo json_encode($json);
5 nó vào cơ sở dữ liệu.


include_once('config.php');
if($_SERVER['REQUEST_METHOD'] == "POST"){
// Get data from the REST client
$task = isset($_POST['task']) ? mysqli_real_escape_string($conn, $_POST['task']) : "";
$date = isset($_POST['date']) ? mysqli_real_escape_string($conn, $_POST['date']) : "";
$priority = isset($_POST['priority']) ? mysqli_real_escape_string($conn, $_POST['priority']) : "";
// Insert data into database
$sql = "INSERT INTO `my_to_do_db`.`my_to_do_tb` (`task`, `date`, `priority`) VALUES ('$task', '$date', '$priority');";
$post_data_query = mysqli_query($conn, $sql);
if($post_data_query){
$json = array("status" => 1, "Success" => "To-Do has been added successfully!");
}
else{
$json = array("status" => 0, "Error" => "Error adding To-Do! Please try again!");
}
}
else{
$json = array("status" => 0, "Info" => "Request method not accepted!");
}
@mysqli_close($conn);
// Set Content-type to JSON
header('Content-type: application/json');
echo json_encode($json);

4. Tạo tập lệnh PHP 1, "Success" => "To-Do has been added successfully!"); } else{ $json = array("status" => 0, "Error" => "Error adding To-Do! Please try again!"); }}else{ $json = array("status" => 0, "Info" => "Request method not accepted!");}@mysqli_close($conn);// Set Content-type to JSONheader('Content-type: application/json');echo json_encode($json);6 để tìm nạp thông tin việc cần làm từ danh sách việc cần làm

Tập lệnh này


include_once('config.php');
if($_SERVER['REQUEST_METHOD'] == "POST"){
// Get data from the REST client
$task = isset($_POST['task']) ? mysqli_real_escape_string($conn, $_POST['task']) : "";
$date = isset($_POST['date']) ? mysqli_real_escape_string($conn, $_POST['date']) : "";
$priority = isset($_POST['priority']) ? mysqli_real_escape_string($conn, $_POST['priority']) : "";
// Insert data into database
$sql = "INSERT INTO `my_to_do_db`.`my_to_do_tb` (`task`, `date`, `priority`) VALUES ('$task', '$date', '$priority');";
$post_data_query = mysqli_query($conn, $sql);
if($post_data_query){
$json = array("status" => 1, "Success" => "To-Do has been added successfully!");
}
else{
$json = array("status" => 0, "Error" => "Error adding To-Do! Please try again!");
}
}
else{
$json = array("status" => 0, "Info" => "Request method not accepted!");
}
@mysqli_close($conn);
// Set Content-type to JSON
header('Content-type: application/json');
echo json_encode($json);
7S dữ liệu từ cơ sở dữ liệu MySQL sử dụng

include_once('config.php');
if($_SERVER['REQUEST_METHOD'] == "POST"){
// Get data from the REST client
$task = isset($_POST['task']) ? mysqli_real_escape_string($conn, $_POST['task']) : "";
$date = isset($_POST['date']) ? mysqli_real_escape_string($conn, $_POST['date']) : "";
$priority = isset($_POST['priority']) ? mysqli_real_escape_string($conn, $_POST['priority']) : "";
// Insert data into database
$sql = "INSERT INTO `my_to_do_db`.`my_to_do_tb` (`task`, `date`, `priority`) VALUES ('$task', '$date', '$priority');";
$post_data_query = mysqli_query($conn, $sql);
if($post_data_query){
$json = array("status" => 1, "Success" => "To-Do has been added successfully!");
}
else{
$json = array("status" => 0, "Error" => "Error adding To-Do! Please try again!");
}
}
else{
$json = array("status" => 0, "Info" => "Request method not accepted!");
}
@mysqli_close($conn);
// Set Content-type to JSON
header('Content-type: application/json');
echo json_encode($json);
2 làm tham số truy vấn yêu cầu. Nói cách khác, tập lệnh này cho phép chúng tôi tìm được việc cần làm từ danh sách bằng cách sử dụng một tác vụ việc cần làm.

Ví dụ: giả sử chúng ta có một việc cần làm tên nhiệm vụ là mã viết. Bây giờ bạn có thể truy xuất thông tin của nó bằng cách sử dụng mã ghi làm chuỗi truy vấn. Thêm về nó được đề cập sau trong bài viết này.Write Code. Now you can retrieve its information by using Write Code as a query string. More on it is covered later in this post.


include_once('config.php');
$task = isset($_GET['task']) ? mysqli_real_escape_string($conn, $_GET['task']) : "";
$sql = "SELECT * FROM `my_to_do_db`.`my_to_do_tb` WHERE task='{$task}';";
$get_data_query = mysqli_query($conn, $sql) or die(mysqli_error($conn));
if(mysqli_num_rows($get_data_query)!=0){
$result = array();

while($r = mysqli_fetch_array($get_data_query)){
extract($r);
$result[] = array("Task" => $task, "Date" => $date, 'Priority' => $priority);
}
$json = array("status" => 1, "info" => $result);
}
else{
$json = array("status" => 0, "error" => "To-Do not found!");
}
@mysqli_close($conn);

// Set Content-type to JSON
header('Content-type: application/json');
echo json_encode($json);

Bây giờ chúng tôi đã thực hiện với mã hóa. Thời gian để xem API của chúng tôi trong hành động.

Thử tạo một việc cần làm

Bây giờ bạn đã viết mã, đã đến lúc kiểm tra API. Đối với điều này, hãy mở khách hàng REST yêu thích của bạn và gửi cuộc gọi


include_once('config.php');
if($_SERVER['REQUEST_METHOD'] == "POST"){
// Get data from the REST client
$task = isset($_POST['task']) ? mysqli_real_escape_string($conn, $_POST['task']) : "";
$date = isset($_POST['date']) ? mysqli_real_escape_string($conn, $_POST['date']) : "";
$priority = isset($_POST['priority']) ? mysqli_real_escape_string($conn, $_POST['priority']) : "";
// Insert data into database
$sql = "INSERT INTO `my_to_do_db`.`my_to_do_tb` (`task`, `date`, `priority`) VALUES ('$task', '$date', '$priority');";
$post_data_query = mysqli_query($conn, $sql);
if($post_data_query){
$json = array("status" => 1, "Success" => "To-Do has been added successfully!");
}
else{
$json = array("status" => 0, "Error" => "Error adding To-Do! Please try again!");
}
}
else{
$json = array("status" => 0, "Info" => "Request method not accepted!");
}
@mysqli_close($conn);
// Set Content-type to JSON
header('Content-type: application/json');
echo json_encode($json);
5 như bên dưới. Bạn cần chắc chắn rằng bạn chọn cơ thể là

include_once('config.php');
$task = isset($_GET['task']) ? mysqli_real_escape_string($conn, $_GET['task']) : "";
$sql = "SELECT * FROM `my_to_do_db`.`my_to_do_tb` WHERE task='{$task}';";
$get_data_query = mysqli_query($conn, $sql) or die(mysqli_error($conn));
if(mysqli_num_rows($get_data_query)!=0){
$result = array();

while($r = mysqli_fetch_array($get_data_query)){
extract($r);
$result[] = array("Task" => $task, "Date" => $date, 'Priority' => $priority);
}
$json = array("status" => 1, "info" => $result);
}
else{
$json = array("status" => 0, "error" => "To-Do not found!");
}
@mysqli_close($conn);

// Set Content-type to JSON
header('Content-type: application/json');
echo json_encode($json);
0 và nhập các giá trị khóa tương ứng. Xem ảnh chụp màn hình để hiểu rõ hơn.Body as

include_once('config.php');
$task = isset($_GET['task']) ? mysqli_real_escape_string($conn, $_GET['task']) : "";
$sql = "SELECT * FROM `my_to_do_db`.`my_to_do_tb` WHERE task='{$task}';";
$get_data_query = mysqli_query($conn, $sql) or die(mysqli_error($conn));
if(mysqli_num_rows($get_data_query)!=0){
$result = array();

while($r = mysqli_fetch_array($get_data_query)){
extract($r);
$result[] = array("Task" => $task, "Date" => $date, 'Priority' => $priority);
}
$json = array("status" => 1, "info" => $result);
}
else{
$json = array("status" => 0, "error" => "To-Do not found!");
}
@mysqli_close($conn);

// Set Content-type to JSON
header('Content-type: application/json');
echo json_encode($json);
0 & enter key-values accordingly. See the screenshot for a better understanding.

Điểm cuối còn lại

{
https://localhost/~admin/REST-TO-DO/add-to-do
}

Nếu bạn đã làm theo các bước một cách chính xác, bạn sẽ thấy rằng việc cần làm của bạn đã được thêm vào cơ sở dữ liệu thành công! Bạn sẽ thấy một đầu ra tương tự như bên dưới.

{
"status": 1,
"Success": "To-Do has been added successfully!"
}

Bạn có thể lấy toàn bộ mã từ kho GitHub của tôi. Nó cũng có tập lệnh


include_once('config.php');
$task = isset($_GET['task']) ? mysqli_real_escape_string($conn, $_GET['task']) : "";
$sql = "SELECT * FROM `my_to_do_db`.`my_to_do_tb` WHERE task='{$task}';";
$get_data_query = mysqli_query($conn, $sql) or die(mysqli_error($conn));
if(mysqli_num_rows($get_data_query)!=0){
$result = array();

while($r = mysqli_fetch_array($get_data_query)){
extract($r);
$result[] = array("Task" => $task, "Date" => $date, 'Priority' => $priority);
}
$json = array("status" => 1, "info" => $result);
}
else{
$json = array("status" => 0, "error" => "To-Do not found!");
}
@mysqli_close($conn);

// Set Content-type to JSON
header('Content-type: application/json');
echo json_encode($json);
1 để tạo cơ sở dữ liệu & bảng của nó. Bạn cũng có thể nhấp vào liên kết bên dưới để đi đến kho lưu trữ.

Tìm nạp thông tin việc cần làm

Để truy xuất thông tin việc cần làm, hãy gọi cuộc gọi


include_once('config.php');
if($_SERVER['REQUEST_METHOD'] == "POST"){
// Get data from the REST client
$task = isset($_POST['task']) ? mysqli_real_escape_string($conn, $_POST['task']) : "";
$date = isset($_POST['date']) ? mysqli_real_escape_string($conn, $_POST['date']) : "";
$priority = isset($_POST['priority']) ? mysqli_real_escape_string($conn, $_POST['priority']) : "";
// Insert data into database
$sql = "INSERT INTO `my_to_do_db`.`my_to_do_tb` (`task`, `date`, `priority`) VALUES ('$task', '$date', '$priority');";
$post_data_query = mysqli_query($conn, $sql);
if($post_data_query){
$json = array("status" => 1, "Success" => "To-Do has been added successfully!");
}
else{
$json = array("status" => 0, "Error" => "Error adding To-Do! Please try again!");
}
}
else{
$json = array("status" => 0, "Info" => "Request method not accepted!");
}
@mysqli_close($conn);
// Set Content-type to JSON
header('Content-type: application/json');
echo json_encode($json);
7 đối với điểm cuối còn lại bên dưới. Lưu ý cách tôi đang sử dụng

include_once('config.php');
$task = isset($_GET['task']) ? mysqli_real_escape_string($conn, $_GET['task']) : "";
$sql = "SELECT * FROM `my_to_do_db`.`my_to_do_tb` WHERE task='{$task}';";
$get_data_query = mysqli_query($conn, $sql) or die(mysqli_error($conn));
if(mysqli_num_rows($get_data_query)!=0){
$result = array();

while($r = mysqli_fetch_array($get_data_query)){
extract($r);
$result[] = array("Task" => $task, "Date" => $date, 'Priority' => $priority);
}
$json = array("status" => 1, "info" => $result);
}
else{
$json = array("status" => 0, "error" => "To-Do not found!");
}
@mysqli_close($conn);

// Set Content-type to JSON
header('Content-type: application/json');
echo json_encode($json);
3 làm tham số truy vấn yêu cầu.

Điểm cuối còn lại

{
https://localhost/~admin/REST-TO-DO/info?task=Write Code
}

Nếu bạn đã làm theo các bước một cách chính xác, bạn sẽ thấy rằng việc cần làm của bạn đã được thêm vào cơ sở dữ liệu thành công! Bạn sẽ thấy một đầu ra tương tự như bên dưới.

{
"status": 1,
"info": [
{
"Task": "Write Code",
"Date": "18/06/2019",
"Priority": "1"
}
]
}

Bạn có thể lấy toàn bộ mã từ kho GitHub của tôi. Nó cũng có tập lệnh


include_once('config.php');
$task = isset($_GET['task']) ? mysqli_real_escape_string($conn, $_GET['task']) : "";
$sql = "SELECT * FROM `my_to_do_db`.`my_to_do_tb` WHERE task='{$task}';";
$get_data_query = mysqli_query($conn, $sql) or die(mysqli_error($conn));
if(mysqli_num_rows($get_data_query)!=0){
$result = array();

while($r = mysqli_fetch_array($get_data_query)){
extract($r);
$result[] = array("Task" => $task, "Date" => $date, 'Priority' => $priority);
}
$json = array("status" => 1, "info" => $result);
}
else{
$json = array("status" => 0, "error" => "To-Do not found!");
}
@mysqli_close($conn);

// Set Content-type to JSON
header('Content-type: application/json');
echo json_encode($json);
1 để tạo cơ sở dữ liệu & bảng của nó. Bạn cũng có thể nhấp vào liên kết bên dưới để đi đến kho lưu trữ.Basic Authentication to this API so that only authenticated users are able to add/fetch the To-Do’s.

Tìm nạp thông tin việc cần làm

Để truy xuất thông tin việc cần làm, hãy gọi cuộc gọi


include_once('config.php');
if($_SERVER['REQUEST_METHOD'] == "POST"){
// Get data from the REST client
$task = isset($_POST['task']) ? mysqli_real_escape_string($conn, $_POST['task']) : "";
$date = isset($_POST['date']) ? mysqli_real_escape_string($conn, $_POST['date']) : "";
$priority = isset($_POST['priority']) ? mysqli_real_escape_string($conn, $_POST['priority']) : "";
// Insert data into database
$sql = "INSERT INTO `my_to_do_db`.`my_to_do_tb` (`task`, `date`, `priority`) VALUES ('$task', '$date', '$priority');";
$post_data_query = mysqli_query($conn, $sql);
if($post_data_query){
$json = array("status" => 1, "Success" => "To-Do has been added successfully!");
}
else{
$json = array("status" => 0, "Error" => "Error adding To-Do! Please try again!");
}
}
else{
$json = array("status" => 0, "Info" => "Request method not accepted!");
}
@mysqli_close($conn);
// Set Content-type to JSON
header('Content-type: application/json');
echo json_encode($json);
7 đối với điểm cuối còn lại bên dưới. Lưu ý cách tôi đang sử dụng

include_once('config.php');
$task = isset($_GET['task']) ? mysqli_real_escape_string($conn, $_GET['task']) : "";
$sql = "SELECT * FROM `my_to_do_db`.`my_to_do_tb` WHERE task='{$task}';";
$get_data_query = mysqli_query($conn, $sql) or die(mysqli_error($conn));
if(mysqli_num_rows($get_data_query)!=0){
$result = array();

while($r = mysqli_fetch_array($get_data_query)){
extract($r);
$result[] = array("Task" => $task, "Date" => $date, 'Priority' => $priority);
}
$json = array("status" => 1, "info" => $result);
}
else{
$json = array("status" => 0, "error" => "To-Do not found!");
}
@mysqli_close($conn);

// Set Content-type to JSON
header('Content-type: application/json');
echo json_encode($json);
3 làm tham số truy vấn yêu cầu.

Làm thế nào tạo API MySQL trong PHP?

Trong bước này, chúng tôi sẽ tạo các điểm cuối API REST REST để chèn hoặc thêm một bản ghi duy nhất trong bảng MySQL. ... Tạo tạo. Tệp PHP trong thư mục API và thêm mã sau ..

Chúng ta có thể tạo API bằng PHP không?

Có nhiều khung tuyệt vời có thể giúp bạn xây dựng API REST một cách nhanh chóng.Nền tảng API của Laravel/lum và Symfony là những ví dụ được sử dụng thường xuyên nhất trong hệ sinh thái PHP.Chúng cung cấp các công cụ tuyệt vời để xử lý các yêu cầu và tạo các phản hồi JSON với mã trạng thái HTTP chính xác.Laravel/Lumen and Symfony's API platform are the most often used examples in the PHP ecosystem. They provide great tools to process requests and generate JSON responses with the correct HTTP status codes.

Làm cách nào để tạo API cơ sở dữ liệu?

Đây là những bước chúng ta cần làm theo:..
Tạo một dự án API Web trống ..
Chọn API Web trong cửa sổ chọn mẫu ..
Tạo bảng và chèn dữ liệu vào bảng cơ sở dữ liệu ..
Thêm một dịch vụ web ..
Thêm mô hình dữ liệu thực thể ADO.NET ..
Tạo kết nối mới với SQL Server ..
Tạo các thực thể từ cơ sở dữ liệu ..
Thêm bộ điều khiển ..

MySQL có API không?

Các đầu nối và API MySQL là trình điều khiển và thư viện mà bạn sử dụng để kết nối các ứng dụng bằng các ngôn ngữ lập trình khác nhau với các máy chủ cơ sở dữ liệu MySQL.Máy chủ ứng dụng và cơ sở dữ liệu có thể nằm trên cùng một máy hoặc giao tiếp trên mạng.. The application and database server can be on the same machine, or communicate across the network.