我已將 Mysql 數(shù)據(jù)庫中的數(shù)據(jù)格式化如下:[start]do this[working]do thatand thadnot this[end]do thisand this我使用這個 PHP 腳本獲取數(shù)據(jù):$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 格式丟失。是否可以將數(shù)據(jù)庫中的格式保留到前端?謝謝
1 回答

牛魔王的故事
TA貢獻1830條經(jīng)驗 獲得超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 關注
- 90 瀏覽
添加回答
舉報
0/150
提交
取消