<?php
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: POST");

require_once "../config.php";

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

$bihua = $data->bihua;

$conn = connect_db();

$res = array();

if ($conn->connect_error) {
	die('Connection failed: ' . $conn->connect_error);
} else {
	$sql = "
		SELECT hanzi, pinyin, kx_bihua, wuxing, xingming 
		FROM `hanzi` 
		WHERE kx_bihua = " . $bihua . "
		ORDER BY pinyin
	";
	$result = $conn->query($sql);

	if ($result->num_rows > 0) {
		while ($row = $result->fetch_assoc()) {
			$temp = array(
				"hanzi" => $row["hanzi"],
				"pinyin" => $row["pinyin"],
				"kx_bihua" => $row["kx_bihua"],
				"wuxing" => $row["wuxing"],
				"xingming" => $row["xingming"],
			);

			array_push($res, $temp);
		}
	}
}

$conn->close();
echo json_encode($res);
