Create a Simple API In PHP with MySQL Database

Create a Simple API In PHP with MySQL Database

Codes Easy

4 года назад

144,460 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

sabTV
sabTV - 17.10.2023 17:09

Great tutorial. Simple and ez to understand. Many tutorial out there are using classes. It make me confused 😵

Ответить
raghavendra jangam
raghavendra jangam - 31.08.2023 14:58

This code gives me an Internal Server 500 Error so I updated the code if anyone getting same error use my code - <?php
$con = mysqli_connect("localhost", "root", "", "{database_name_isert_here}");
$response = array();

if ($con) {
$sql = "SELECT * FROM {table_name_insert_here}";
$result = mysqli_query($con, $sql);

if ($result) {
// Set the correct Content-Type header
header("Content-Type: application/json");

$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
$response['data'] = $rows; // Store the data in an associative array
echo json_encode($response, JSON_PRETTY_PRINT);
} else {
// Handle query execution error
$errorResponse = array('error' => 'Query execution failed');
echo json_encode($errorResponse);
}

// Close the database connection
mysqli_close($con);
} else {
// Handle database connection error
$errorResponse = array('error' => 'Database connection failed');
echo json_encode($errorResponse);
}
?>

Ответить
Guru Prakash
Guru Prakash - 19.04.2023 13:40

please make a video on information about MySql, Sqlite what is the need and difference between
and what is php plugins?

Ответить
chandrashekar kankanala
chandrashekar kankanala - 02.03.2023 10:27

where is the code

Ответить
MulhamReacts
MulhamReacts - 27.02.2023 13:31

how to do a post rquest?

Ответить
Kapitan Oppa
Kapitan Oppa - 08.02.2023 03:09

do you have an actual tutorial that connects to a thrid party api

Ответить
Jiés
Jiés - 24.01.2023 01:06

aahhh thanks you guyz completely wrecked my 2 hour , in the line no 5 there `data` should bein back thick 😤😤😤😤

Ответить
A
A - 16.07.2022 10:48

thank you bro

Ответить
Arpita Saxena
Arpita Saxena - 11.07.2022 13:37

Bro this is not a API its just a Database query

Ответить
Great Programmer
Great Programmer - 22.06.2022 20:29

This is good but how to protect of other people, every guy who know how to use fetch in javascript can get data from db using this method. This isnt secured bro.

Ответить
Usman Khawar
Usman Khawar - 25.05.2022 11:43

Good Tutorial Bro.

Is there a way that instead of calling all the data. We can only get one we required. Creating an API that somehow asks to enter the name and will only return data containing that name

Thanks

Ответить
Jeff
Jeff - 15.05.2022 10:37

This is not an api... lots of paid commenters.

Ответить
Usaid Yousuf
Usaid Yousuf - 20.03.2022 13:12

Very informative & to the point
Helped me a lot in making an API that I want to use with ReactJS

Ответить
Foad Mohtadi
Foad Mohtadi - 16.03.2022 12:14

Is this API???? you've just connected mySql. Api is different :)

Ответить
Dislike Counter
Dislike Counter - 27.02.2022 19:15

mslqi code

<?php
$mysqli = new mysqli("localhost","root","","API_DATA");
$response = array();
if ($mysqli->ping())
{
$db = "select * from data";
$result = mysqli_query($mysqli,$db);
if($result){
header("Content-Type: JSON");
$i=0;
while($row = mysqli_fetch_assoc($result)){
$response[$i]['id'] = $row['id'];
$response[$i]['name'] = $row['name'];
$response[$i]['age'] = $row['age'];
$response[$i]['email'] = $row['email'];

$i++;
}
echo json_encode($response,JSON_PRETTY_PRINT);
}
}
?>

Ответить
Wan
Wan - 26.12.2021 06:05

I love you

Ответить
Satinder Satsangi
Satinder Satsangi - 17.12.2021 06:37

Really Easy to understand, good work

Ответить
Ama Junriel Damalan
Ama Junriel Damalan - 02.12.2021 16:11

when I run the file, it says "Not found". why?

Ответить
Eng Ximaani
Eng Ximaani - 02.12.2021 10:38

Thanks dear sir

Ответить
Christ Bryan
Christ Bryan - 28.11.2021 12:20

Thanks for your tutorial very easy to understand.

Ответить