我已将 Mysql 数据库中的数据格式化如下:[start]do this[working]do thatand thadnot this[end]do thisand this我使用这个 PHP 脚本获取数据:$result = mysqli_query($conn, "SELECT * FROM ....");$data = array();while($row = mysqli_fetch_assoc($result)){$data[] = $row;}echo json_encode($data);然后我在前端显示它:json[0].columnName我得到的是:[start] do this [working] do that and thad not this [end] do this and this我认为由于使用 json 格式丢失。是否可以将数据库中的格式保留到前端?谢谢
1 回答

牛魔王的故事
TA贡献1830条经验 获得超3个赞
我尝试在本地模拟您的问题
<?php
$mysqli = new mysqli("localhost","root","root","stackoverflow");
$result = mysqli_query($mysqli, "SELECT * FROM data");
$data = array();
while($row = mysqli_fetch_assoc($result))
{
$data[] = $row;
}
?>
<div id="test"></div>
<script>
var data = <?php echo json_encode($data); ?>;
console.log(data );
document.getElementById('test').innerHTML = data[0].Location.split('\n').join('<br>');
</script>
按预期输出:
[start]
do this
[working]
do that
and thad
not this
[end]
do this
and this
所以你需要json[0].columnName.split('\n').join('<br>')在你的js代码中做:)
- 1 回答
- 0 关注
- 93 浏览
添加回答
举报
0/150
提交
取消