gpt4 book ai didi

javascript - 使用 mysql 查询从单列返回值

转载 作者:行者123 更新时间:2023-11-29 21:04:47 27 4
gpt4 key购买 nike

我有一个 php 函数,它将从 users 数据库中的列中获取名称列表。我想要做的是从列 name 中获取所有值并将其插入到数组中。

我在 php 方面所做的是:

header('Content-type: application/json');
include ('../Core/Initialization.php');

$courseName = $_POST['courseName'];
$semester = $_POST['semester'];

$sql = mysql_query("SELECT DISTINCT `name` FROM `users` WHERE `programme` = '$courseName' AND `semester` = '$semester'") or trigger_error(mysql_error().$sql);
$column = mysql_fetch_assoc($sql);

$arr = array();
foreach($column as $value) {
$arr[] = array('name' => $value['name']); //I have tried it this way but it didn't work when I try to display the values.
}

echo json_encode($arr);//I have tried to remove the array and just json_encode($column). I have successfully print out the first values, but fail to print out the next values collected from the column.

将处理/打印出名称的js函数:

function nameProcess(data) {
alert(data.name); //This will only display the full values from the first(?) index

nameArray = data.name;

for (var i=0; i < nameArray.length; i++) {
alert(nameArray[i]); //But, this loop only displays one character each time of the alert. Example: Each character from the word "Hello" will show up one by one as alert.
}
}

});

有没有更好的方法来做到这一点?我想要做的是将 name 列中的所有值导出到一个数组中,并将其每个值作为选择框的选项进行迭代。但现在,我该如何解决这个问题呢?

最佳答案

First of all, don't use mysql_* functions as they are deprecated and removed totally PHP 7.

回到你的问题,你可以只用MySQL循环来获取mysql多维数组。

更正的代码:

$sql = mysql_query("SELECT DISTINCT `name` FROM `users` WHERE `programme` = '$courseName' AND `semester` = '$semester'") or trigger_error(mysql_error().$sql);
$res = mysql_query($sql); // Missing this.
$column = ;
$arr = array();
while ($value = mysql_fetch_assoc($res)) {
$arr[] = $value['name'];
}
echo json_encode($arr);

关于javascript - 使用 mysql 查询从单列返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36909963/

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