<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";
$MYNAME=$_GET["myname"];//接收get传递的值
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// header("Content-Type:text/html;charset=utf-8");
// echo "连接成功<br>";
// echo $MYNAME; //将值通过ajax get发送到php页面
$sql = "SELECT id, name, age FROM tables";
// $sql = "SELECT * FROM tables WHERE name='$MYNAME'"; //查询语句
$result = mysqli_query($conn, $sql);
$arr=array(); //开始转换
while ($row = mysqli_fetch_assoc($result)) { //从结果集中取得一行作为关联数组
$arr[] = $row;
}
echo json_encode($arr,JSON_UNESCAPED_UNICODE);
//json_encode是转换成json字符串 第二个参数是解决中文乱码问题
mysqli_close($conn);
?>