PHP에서 MySQL의 데이터를 JSON 으로 변환하기 !
<?php
// Initialize variable for database credentials
$dbhost = 'hostname';
$dbuser = 'username';
$dbpass = 'password';
$dbname = 'sakila';
//Create database connection
$dblink = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
//Check connection was successful
if ($dblink->connect_errno) {
printf("Failed to connect to database");
exit();
}
//Fetch 3 rows from actor table
$result = $dblink->query("SELECT * FROM actor LIMIT 3");
//Initialize array variable
$dbdata = array();
//Fetch into associative array
while ( $row = $result->fetch_assoc()) {
$dbdata[]=$row;
}
//Print array in JSON format
echo json_encode($dbdata);
?>
출처 : https://www.opentechguides.com/how-to/article/php/100/mysql-to-json.html
'IT > Programming' 카테고리의 다른 글
C#과 PHP에서 같은 암호화/복호화 사용하기 (1) | 2019.03.28 |
---|---|
Convert Json to DataTable in C# (0) | 2019.03.28 |
C# Postgresql Helper Class (0) | 2019.03.21 |
C# MySQL Helper Class (0) | 2018.07.26 |
C#에서 SSH로 원격(Remote)에 있는 MySql 접속하기 (0) | 2018.07.26 |