从mysql

时间:2017-03-14 22:50:50

标签: php mysql json ajax

我有数据库,我只是从中检索一个单元格,下面是一个例子:

+------+
|Failed|
+------+
|325   |
+------+

我正在尝试将此值添加到我的html文件中的多个区域,但是我无法这样做,下面是html文件:

JSON:

$(document).ready(function() {

$.getJSON('PHP/Faild.php', function(GetFailedRenew) {
        // set the html content of the id myThing to the value contained in data
        $("#myThing").html(data.value);
     });   
});

html正文样本:

<div class="inner">
<h3>150</h3>
<p id="myThing"></p>
</div>

Failed.php

<?php

$DB_NAME = 'test';
$DB_HOST = 'localhost';
$db_port = '3306';
$DB_USER = 'root';
$DB_PASS = 'mysql';

$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);

  if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
  }



  $result = $mysqli->query('SELECT RenewFailed FROM Statistics where id=(select max(id) from Statistics);');
  $rows = array();
  $table = array();
  $table['cols'] = array(
  array('label' => 'RenewFailed', 'type' => 'number')

);
    foreach($result as $r) {

      $temp = array();

      $temp[] = array('v' => (string) $r['RenewFailed']); 
      $rows[] = array('c' => $temp);
    }

$table['rows'] = $rows;

// convert data into JSON format
$SubsStats = json_encode($table);
echo $SubsStats;

?>

这是failed.php的输出:

{"cols":[{"label":"RenewFailed","type":"number"}],"rows":[{"c":[{"v":"325"}]}]}

我是PHP和JSON的新手所以请告诉我是否有其他/更好的方法来实现上述目标..

感谢 阿里

1 个答案:

答案 0 :(得分:0)

http://api.jquery.com/jquery.getjson/将data.value更改为此GetFailedRenew,例如