如何从运行PHP和MySQL的Web服务器读取JSON数据? [英] How to reads JSON data from a web server running PHP and MySQL?

查看:83
本文介绍了如何从运行PHP和MySQL的Web服务器读取JSON数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从运行PHP和MySQL的Web服务器读取JSON数据。

我从 http://www.w3schools.com/json/json_example.asp

然后我用我的urlm改变了他们的网址http://localhost/demo.php

但它不起作用。我是JSON的初学者,我不知道我的代码发生了什么。请帮帮我,谢谢。

这是我的PHP代码:

 <?php  
$ servername = 本地主机;
$ username = root;
$ password = ;
// 创建连接
$ conn = new mysqli($ servername,$ username,$ password);
// 检查连接
if ($ conn-> connect_error)
{
die( Connection失败:。$ conn-> connect_error);
}
$ db_selected = mysqli_select_db($ conn, test_json);
if(!$ db_selected)
{
die( Khôngthểsửdụng数据库: .mysql_error());
}
$ sql = SELECT * from data;
$ result = $ conn - >查询($ SQL);
$ outp = [;
while($ rs = $ result-> fetch_array(MYSQLI_ASSOC)){
if ($ outp!= [){$ outp。= ;}
$ outp。= ' {Name :'。$ rs [ 名称]。' ,';
$ outp。= ' 年龄:'。$ rs [ 年龄]。' ;
$ outp。= ' 地址:'。$ rs [ 地址]。' ;
$ outp。= ' Phone:'。$ rs [ 电话]。' };
}
$ outp。= ];
$ conn-> close();
echo($ outp);
?>



这是我的JSON

< pre lang =Javascript>< h1>客户< / h1 >
< div id = id01 > < / div >

< script>
var xmlhttp = new XMLHttpRequest();
var url = http://本地主机:80 / demo.php;


xmlhttp.onreadystatechange = function ()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200 ){
myFunction(xmlhttp.responseText);
}
}
xmlhttp.open( GET, url, true );
xmlhttp.send();

function myFunction(response){
var arr = < span class =code-sdkkeyword> JSON
.parse(response);
var i;
var out = < table> ;

for (i = 0 ; i< arr.length; i ++ ){
out + = < tr>< td> +
arr [i] .Name +
< / td>< td> +
arr [i] .Age +
< / td>< td> +
arr [i] .Address +
< / td>< td> +
arr [i] .Phone +
< / TD>< / TR GT&;;
}
out + = < / table>
document .getElementById( id01) .innerHTML = out;
}
< / script>

解决方案

servername = localhost;


用户名 = ;


密码 = ;
// 创建连接

I try to reads JSON data from a web server running PHP and MySQL.
I learn code from http://www.w3schools.com/json/json_example.asp
And then I I changed their url with my urlm like this http://localhost/demo.php
But it does not work. I am beginner in JSON and I don't know what happened with my code. Please help me, thank.
Here is my PHP code:

<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername,$username,$password);
//check connection
if ($conn->connect_error) 
{
    die("Connection failed: " . $conn->connect_error);
}
$db_selected = mysqli_select_db($conn,"test_json");
if(!$db_selected)
{
	die("Không thể sử dụng DATABASE: ".mysql_error());
}
$sql = "SELECT * from data";
$result = $conn -> query($sql);
$outp = "[";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
    if ($outp != "[") {$outp .= ",";}
    $outp.='{"Name":"'.$rs["Name"].'",';
    $outp.='"Age":"'.$rs["Age"].'",';
    $outp.='"Address":"'.$rs["Address"].'",';
	$outp.='"Phone":"'.$rs["Phone"].'"}';
}
$outp.="]";
$conn->close();
echo($outp);
?>


And here is my JSON

<h1>Customers</h1>
<div id="id01"></div>

<script>
var xmlhttp = new XMLHttpRequest();
var url = "http://localhost:80/demo.php";


xmlhttp.onreadystatechange=function() 
{
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        myFunction(xmlhttp.responseText);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();

function myFunction(response) {
    var arr = JSON.parse(response);
    var i;
    var out ="<table>";

    for(i = 0; i < arr.length; i++) {
        out +="<tr><td>" +
        arr[i].Name +
        "</td><td>" +
        arr[i].Age +
        "</td><td>" +
        arr[i].Address +
        "</td><td>"+
		arr[i].Phone +
        "</td></tr>";
    }
    out+="</table>"
    document.getElementById("id01").innerHTML = out;
}
</script>

解决方案

servername = "localhost";


username = "root";


password = ""; // Create connection


这篇关于如何从运行PHP和MySQL的Web服务器读取JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆