Showing posts with label json. Show all posts
Showing posts with label json. Show all posts

Tuesday, 29 September 2020

PHP MYSQL TO JSON

https://www.opentechguides.com/how-to/article/php/100/mysql-to-json.html

<?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);
 
?>

Result:


[
	{
	"actor_id":"1",
	"first_name":"PENELOPE",
	"last_name":"GUINESS",
	"last_update":"2006-02-15 04:34:33"
	},
	{	
	"actor_id":"2",
	"first_name":"NICK",
	"last_name":"WAHLBERG",
	"last_update":"2006-02-15 04:34:33"
	},
	{
	"actor_id":"3",
	"first_name":"ED",
	"last_name":"CHASE",
	"last_update":"2006-02-15 04:34:33"
	}
]

As you can see, the result is a valid JSON. You may use this in combination with Jquery/AJAX to pass database data to a web application that expects data in JSON.

Saturday, 23 February 2019

js : parse json COMPLETED

<!DOCTYPE html>
<html>
<body>
<script>

var gethttp = new XMLHttpRequest();
gethttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    var load = JSON.parse(this.responseText);
    //select from json
    var x = load.nama;
    var y = load.task;
    // var y = load.pets[1].name;
    //render and to html
    document.querySelector("#nama").innerHTML = x;
    document.querySelector("#alamat").innerHTML = y;
  }
};
// gethttp.open("GET", "./numerologi/json.txt", true);
gethttp.open("GET", "seed/sprout", true);
gethttp.send();
</script>


<b id="nama"></b></br>
<b id="alamat"></b>
</body>
</html>

//htaccess for apache

RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.txt [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.txt -f
RewriteRule ^ %{REQUEST_URI}.txt [NC,L]

PHP : convert to json

Many web applications expect external data in JSON format, hence converting MySQL data to JSON is something that web developers encounter on a regular basis. JSON (JavaScript Object Notation) is fast becoming the most popular data format in server/browser communication. It is light weight, human readable and easy to generate and parse.
Data from MySQL database can be easily converted into JSON format using PHP. The below example uses Sakila sample database that comes with standard MySQL installation. It fetches the first 3 rows of actor table into an associative array using mysqli_fetch_assoc(). Then the array is encoded into JSON using json_encode.
<?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);
 
?>

Result:


[
 {
 "actor_id":"1",
 "first_name":"PENELOPE",
 "last_name":"GUINESS",
 "last_update":"2006-02-15 04:34:33"
 },
 { 
 "actor_id":"2",
 "first_name":"NICK",
 "last_name":"WAHLBERG",
 "last_update":"2006-02-15 04:34:33"
 },
 {
 "actor_id":"3",
 "first_name":"ED",
 "last_name":"CHASE",
 "last_update":"2006-02-15 04:34:33"
 }
]
As you can see, the result is a valid JSON. You may use this in combination with Jquery/AJAX to pass database data to a web application that expects data in JSON.

Tembus Gmail dengan TXT record

Type: TXT Name: @ / domainname.com Loadtime : 14400 Value: v=spf1 a mx ip4:SERVER_IP include:netkl.org ~all