0

I have a table set up in a mysql database.. firstName, lastName, email, password, allow, allow.

I loaded a fake person into and i'm testing XMLHttpRequest() funcionality.

For some reason instead of returning data like this:

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

It's returning data like this:

[{"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"}]

I can't figure out why it's duplicating and numbering values. Here is my super simple 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>

And here are my php files (one calls a function in another):

First:

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

Second:

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;
    }

I've been using session variables up to this point, but I would like to start learning how to do things asynchronously and this is all new to me.

Thank you.

  • [That's what you get back from the `fetchAll` API](http://php.net/manual/en/pdostatement.fetchall.php) – Pointy Jan 21 '18 at 19:06
  • 1
    Downvoters please explain. I am new with this ajax and JSON. I have read through all the literature and it turns out that it was not an ajax problem as evidenced by the answers I received. I provided a complete explanation of my issue, and I also provided code explaining my issue. Why is this being downvoted? Its extremely discouraging especially when no explanation is provided. That's why SO get a bad reputation among new programmers. –  Jan 21 '18 at 19:16
  • ...on the other hand, it *could* be argued that a careful reading of the fetchAll() man page would have given the game away. A more explicit explanation wouldn't have gone amiss, however. I for one often need things explained to me in detail :-) – LSerni Jan 21 '18 at 19:22

1 Answers1

1

fetchAll() returns the columns by both name and index by default, see here, you have to specify one if you don't want that.
To only get the names use,

$getResults = $stmt->fetchAll(PDO::FETCH_ASSOC);
Musa
  • 96,336
  • 17
  • 118
  • 137