• 返回json数据
  • php获取mysql数据

返回json数据

1
echo json_encode(array('index'=>$_GET['index']));

php获取mysql数据

第一种

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//var_dump($_POST);
$post = $_POST['hidden'];
echo $post;
$mysqli = new mysqli('localhost','root','123456','test');
if($mysqli->connect_error){
echo $mysqli->connect_error;
exit;
}
//$query="insert INTO img(imgSrc) values(".$_POST['hidden'].") ";//构建查询语句
$query="INSERT INTO img(imgSrc)VALUES('$post')";
$bool = $mysqli->query($query);
/*
while($result_row=mysql_fetch_row(($result)))//取出结果并显示
{
$num=$result_row[0];
$name=$result_row[1];
echo "id:".$num."<br>";
echo "name:".$name."<br>";

}*/

第二种

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$mysqli = new mysqli('localhost','root','123456','test');
//var_dump($mysqli);
$mysqli->query('SET NAMES UTF8');
$sql = "SELECT * FROM msg";
$mysqli_result = $mysqli->query($sql);
//var_dump($mysqli_result);
$arr = [];
while($row = $mysqli_result->fetch_array(MYSQL_ASSOC)){
//var_dump($row);
$arr[$row['id']] = $row;
}
//var_dump($arr);
//json_encode($arr);
echo json_encode($arr);

$mysqli->close();