我有一个脚本,可以读取 php 中表中的所有行并以 json_encode 显示结果。当我想列出列表中的所有行时,它们不会显示。如何显示我的所有记录。我的表有 4 个字段 id
、Name
、Surname
和 Age
。我希望链接具有 id,这样当用户单击时,他们将查看一条记录。
PHP
<?php
require_once("database.php");
$mydb = new MySQLDatabase();
$sql = $mydb->query("Select * from tblprofile");
$rows = array();
$r = $mydb->fetch_array($sql);
$rows[] = $r;
echo json_encode($rows);
?>
HTML
<div data-role="page" id="listofusers" data-theme="a">
<div data-role="header" data-theme="a" data-position="fixed">
<h1>Test</h1>
</div>
<div data-role="content" data-theme="a" id="listofusers_content">
<ul data-role="listview" data-theme="a">
<script type="text/javascript">
$(document).ready(function(){
$.getJSON('http://localhost/v_warehouse_1/inc/getprofiles.php', function(data) {
$.each(data, function(key, val) {
$('ul').append('<li id="' + key + '">' + val.Tip + '</li>');
});
});
});
</script>
</ul>
</div>
<div data-role="footer" data-theme="a" data-position="fixed">
<h4>Test</h4>
</div>
</div>
最佳答案
您可以使用 fetch_all 方法( http://php.net/manual/en/mysqli-result.fetch-all.php ):
require_once("database.php");
$mydb = new MySQLDatabase();
if ($result = $mydb->query("SELECT * FROM tblprofile")) {
$rows = $result->fetch_all(MYSQLI_ASSOC); // eventually MYSQLI_NUM
echo json_encode($rows);
}
关于php - json php调用错误: undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29968959/