gpt4 book ai didi

javascript - JSON.parse 返回单个键的多个值

转载 作者:行者123 更新时间:2023-12-01 02:30:02 24 4
gpt4 key购买 nike

我在 mysql 数据库中设置了一个表..名字、姓氏、电子邮件、密码、允许、允许。

我加载了一个假人并正在测试 XMLHttpRequest() 功能。

由于某种原因而不是返回这样的数据:

[{"firstName":"Theodore","lastName":"Roosevelt","email":"teddyR@gmail.com","password":"12345678","active":"N","allow":"Y"}]

它返回的数据如下:

[{"firstName":"Theodore","0":"Theodore","lastName":"Roosevelt","1":"Roosevelt","email":"teddyR@gmail.com","2":"teddyR@gmail.com","password":"12345678","3":"12345678","active":"N","4":"N","allow":"Y","5":"Y"}]

我不明白为什么它会重复和编号值。这是我的 super 简单的 html/js:

<body>
<div id="results">
<button type="button">Get Data</button>
</div>
</body>

<script>
$("button").click(function(){
$.ajax({url: "selectTest.php", success: function(result){
$("#results").html(result);
}});
});
</script>

这是我的 php 文件(一个在另一个中调用函数):

第一:

<?php
include('config.php');
include('web_db_operations.php');
$email = 'teddyR@gmail.com';
echo getAllUserInfo_userTable($email);
?>

第二:

function getAllUserInfo_userTable(string $email){
include 'config.php';
$sql = 'SELECT * FROM userTable WHERE email= :email';
$stmt = $conn->prepare($sql);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
$stmt->execute();
$getResults = $stmt->fetchAll();
$myJSON = json_encode($getResults);
return $myJSON;
}

到目前为止,我一直在使用 session 变量,但我想开始学习如何异步执行操作,这对我来说是全新的。

谢谢。

最佳答案

fetchAll() 默认情况下按名称和索引返回列,请参阅 here ,如果您不希望这样,则必须指定一个。
仅获取名称使用,

$getResults = $stmt->fetchAll(PDO::FETCH_ASSOC);

关于javascript - JSON.parse 返回单个键的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48370570/

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