以自定义格式编码 php 个记录集
encode php recordset in custom format
我目前有一个用 Dreamweaver 创建的记录集,并将结果编码为 json 格式,工作正常。
记录集
$maxRows_rs_feeds = 3;
$pageNum_rs_feeds = 0;
if (isset($_GET['pageNum_rs_feeds'])) {
$pageNum_rs_feeds = $_GET['pageNum_rs_feeds'];
}
$startRow_rs_feeds = $pageNum_rs_feeds * $maxRows_rs_feeds;
mysql_select_db($database_vivalooks, $vivalooks);
$query_rs_feeds = "SELECT fname,lname FROM profile";
$query_limit_rs_feeds = sprintf("%s LIMIT %d, %d", $query_rs_feeds, $startRow_rs_feeds, $maxRows_rs_feeds);
$rs_feeds = mysql_query($query_limit_rs_feeds, $vivalooks) or die(mysql_error());
$row_rs_feeds= mysql_fetch_assoc($rs_feeds);
if (isset($_GET['totalRows_rs_feeds'])) {
$totalRows_rs_feeds = $_GET['totalRows_rs_feeds'];
} else {
$all_rs_feeds = mysql_query($query_rs_feeds);
$totalRows_rs_feeds = mysql_num_rows($all_rs_feeds);
}
$totalPages_rs_feeds = ceil($totalRows_rs_feeds/$maxRows_rs_feeds)-1;
do {
echo json_encode($row_rs_feeds);
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
mysql_free_result($rs_feeds);
记录集结果
{"fname":"Benjamin","lname":"Blay"}{"fname":"Alfread","lname":"Mark"}{"fname":"yaa","lname":"tiwaa"}
但我更希望结果像这样编码
[{"fname":"Benjamin","lname":"Blay"},{"fname":"Alfred","lname":"Mark"},{"fname":"yaa","lname":"tiwaa"}]
尝试:
do {
$rows[] = $row_rs_feeds;
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
echo json_encode($rows);
尝试
do {
$json[]=json_encode($row_rs_feeds);
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
echo "[".implode(", ", $json)."]";
或
do {
$rows[] = $row_rs_feeds;
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
echo json_encode($rows);
您可以将循环更改为如下所示:
echo "[";
$count = 0;
do {
if($count !== 0)
echo ",";
$count++;
echo json_encode($row_rs_feeds);
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
echo "]";
$count 只是为了跳过第一个 "," 所以你不会得到 [{...},{...}] 或者你可以把它写成 false/true...如果你有 10k 步循环,我现在不记得什么更好...
我目前有一个用 Dreamweaver 创建的记录集,并将结果编码为 json 格式,工作正常。
记录集
$maxRows_rs_feeds = 3;
$pageNum_rs_feeds = 0;
if (isset($_GET['pageNum_rs_feeds'])) {
$pageNum_rs_feeds = $_GET['pageNum_rs_feeds'];
}
$startRow_rs_feeds = $pageNum_rs_feeds * $maxRows_rs_feeds;
mysql_select_db($database_vivalooks, $vivalooks);
$query_rs_feeds = "SELECT fname,lname FROM profile";
$query_limit_rs_feeds = sprintf("%s LIMIT %d, %d", $query_rs_feeds, $startRow_rs_feeds, $maxRows_rs_feeds);
$rs_feeds = mysql_query($query_limit_rs_feeds, $vivalooks) or die(mysql_error());
$row_rs_feeds= mysql_fetch_assoc($rs_feeds);
if (isset($_GET['totalRows_rs_feeds'])) {
$totalRows_rs_feeds = $_GET['totalRows_rs_feeds'];
} else {
$all_rs_feeds = mysql_query($query_rs_feeds);
$totalRows_rs_feeds = mysql_num_rows($all_rs_feeds);
}
$totalPages_rs_feeds = ceil($totalRows_rs_feeds/$maxRows_rs_feeds)-1;
do {
echo json_encode($row_rs_feeds);
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
mysql_free_result($rs_feeds);
记录集结果
{"fname":"Benjamin","lname":"Blay"}{"fname":"Alfread","lname":"Mark"}{"fname":"yaa","lname":"tiwaa"}
但我更希望结果像这样编码
[{"fname":"Benjamin","lname":"Blay"},{"fname":"Alfred","lname":"Mark"},{"fname":"yaa","lname":"tiwaa"}]
尝试:
do {
$rows[] = $row_rs_feeds;
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
echo json_encode($rows);
尝试
do {
$json[]=json_encode($row_rs_feeds);
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
echo "[".implode(", ", $json)."]";
或
do {
$rows[] = $row_rs_feeds;
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
echo json_encode($rows);
您可以将循环更改为如下所示:
echo "[";
$count = 0;
do {
if($count !== 0)
echo ",";
$count++;
echo json_encode($row_rs_feeds);
} while ($row_rs_feeds = mysql_fetch_assoc($rs_feeds));
echo "]";
$count 只是为了跳过第一个 "," 所以你不会得到 [{...},{...}] 或者你可以把它写成 false/true...如果你有 10k 步循环,我现在不记得什么更好...