php - 使用 PHP、JSON 并在 HTML 上显示的 MySQL 查询

标签 php html mysql json cordova

我是一名“自学”编码员,试图让 php 在 HTML 中工作,以便能够在 phonegap 中为我的 web 应用程序的移动版本运行它。我已经按照教程无济于事。 下面附上 php 文件和 html 文件的代码。

     <?php
header("Content-Type: application/json");
if(isset($_POST['limit'])){
    $limit = preg_replace('#[^0-9]#', '', $_POST['limit']);


    $dbhost = '';
    $dbuser = '';
    $dbpass = '';
    $db = '';
    $tbl_name="Mindful Tips";

    $conn = mysql_connect($dbhost,$dbuser,$dbpass);
    mysql_select_db($db);

    $i = 0;
    $jsonData = "{";

    $sqlString = "SELECT * FROM $tbl_name ORDER BY RAND() LIMIT $limit";
    $query = mysql_query($sqlString) or die (mysql_error());

    while ($row = mysql_fetch_array($query)) {
        $i++;
        $ID = $row["ID"];
        $Tip = $row["Tip"];
        $jsonData .= '"tip'.$i.'":{ "ID":"'.$ID.'","Tip":"'.$Tip.'" },';
    }
    //$jsonData = chop($jsonData, ",");
    $jsonData .= '"arbitrary":{"itemcount":'.$i.', "returntime":"'.getdate().'"}';
    $jsonData .= '}';
    echo $jsonData;
    }
    ?>

HTML文件

    <!DOCTYPE HTML>
    <html>
    <head>
    <title>Mindful Tips</title>
    <style type="text/css">
        div#databox {
            padding: 12px;
            background: #F3F3F3;
            border: #CCC 1px solid;
            width:550px;
            height:400px;
        }
    </style>
    <script>
        function onBodyLoad() {
            document.addEventListener("deviceready",onDeviceReady,false);
        }
    </script>
    <script type="text/javascript">
    function ajax_json_data(){
        var databox = document.getElementById("databox");
        var arbitrarybox = document.getElementById("arbitrarybox");
        var hr = new XMLHttpRequest();
        hr.open("POST", "json_mysql_data.php", true);
        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        hr.onreadystatechange = function() {
            if(hr.readyState == 4 && hr.status == 200) {
                var d = JSON.parse(hr.responseText);
                arbitrarybox.innerHTML = d.arbitrary.returntime;
                databox.innerHTML = "";
                for(var o in d){
                    if(d[o].Tip){
                        databox.innerHTML += '<p>'+d[o].Tip+'</p>';
                    }
                }
            }
        }
        hr.send("limit=1");
        databox.innerHTML = "requesting...";
    }
    </script>
    </head>
    <body onload="onBodyLoad()">
    <script type="text/javascript" src="js/FbLogin.js"></script>
                            <div id="databox"></div>
                            <div id="arbitrarybox"></div>
                            <script type="text/javascript">ajax_json_data();</script>
    </body>
    </html>

如有任何帮助,我们将不胜感激。

最佳答案

在 php 中不需要这样制作 JSON 数据。

您可以使用json_encode() 函数将您的数组转换为JSON 字符串,然后打印它。

代码片段..

`     <?php
        header("Content-Type: application/json");

        if(isset($_POST['limit'])){
$limit = preg_replace('#[^0-9]#', '', $_POST['limit']);


$dbhost = '';
$dbuser = '';
$dbpass = '';
$db = '';
$tbl_name="Mindful Tips";

$conn = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($db);

$i = 0;


$sqlString = "SELECT * FROM $tbl_name ORDER BY RAND() LIMIT $limit";
$query = mysql_query($sqlString) or die (mysql_error());
$data=array()
while ($row = mysql_fetch_array($query)) {

    $ID = $row["ID"];
    $Tip = $row["Tip"];
    $data[$i]=array($ID,$Tip);
    $i++;

}
$jsonData=json_encode($data);
echo $jsonData;
}
?>`

关于php - 使用 PHP、JSON 并在 HTML 上显示的 MySQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21502374/

相关文章:

php - 从 twitter API 返回的 JSON 中获取特定值

PHP5 : SplObjectStorage garbage collection

javascript - HTML/Javascript 等待图形绘制

javascript - 如何在按钮 JQM + HTML5 内对齐图像?

mysql - 如何向 Schema 类的 DBIx 搜索方法提供附加参数

java - 尝试在 MySQL 数据库中保存时出现异常

php - 消息系统。多个收件人,正在检索数据。最佳实践

php - 1045 MySQL 错误 当它应该工作时

javascript - 5 秒后提交文本值

mysql - 如何将此准备语句转换为在 Wordpress $wpdb 中使用占位符?