gpt4 book ai didi

php - 如何使用 ajax 从 mysql 查询向 jquery 表添加行?

转载 作者:行者123 更新时间:2023-11-30 22:26:39 25 4
gpt4 key购买 nike

我需要从 AJAX 将行插入到 jQuery 数据表中。在 AJAX 中,当我查询我的 mySql 数据库时,我调用了一个 PHP 文件,但我只在行中显示了一个字符。是因为返回的格式不正确,我无法解析正确的格式。

$query = "..myquery..";
if ($row = mysql_fetch_array($sql)) {
do {
$arr []= $row['name'];
} while ($row = mysql_fetch_array($sql));
echo json_encode($arr); // Here I tried return array without json_encode and a lot of things...
}

我知道用 .DataTable().row.add() 添加行的格式如下,但我没有得到所需的格式。

[["Element software"], ["Software dist"],["Global envir"], ["Software"], ["Software list"]] 

我怎样才能在回显中得到这个格式来返回这个?谢谢!

最佳答案

它是数组的数组,所以你需要将一个数组推送到$arr,而不是一个字符串:

$query = "..myquery..";
$arr = []; // init an empty array
// no need to do if() and do{}while() inside. You can just while(){} it
while($row = mysql_fetch_array($sql)){
$arr[]= [$row['name']]; // <== these square brackets do the trick
}
echo json_encode($arr); // still need to json_encode

关于php - 如何使用 ajax 从 mysql 查询向 jquery 表添加行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35013258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com