0

I'm attempting to run a pdo query on a button click but I'm not getting any results back.

I have similar things like this in other files but I can't get this one to work. The page is refreshing but no data is showing. I'm assuming that the session array isn't being created but I'm not skilled enough in php to tell what is going on.

Index.php

<?php
    session_start();
?>

<a href="#" id="show-data">

<table>
    <thead>
        <tr>
            <th>Field One</th>
            <th>Field Two</th>
            <th>Field Three</th>
        </tr>
    </thead>
    <tbody>
    <?php
        for ($i = 0; $i < count($_SESSION['data']); $i++)
        {
            echo '
                <tr>
                    <td>' . $_SESSION['data'][$i]['FieldOne'] . '</td>
                    <td>' . $_SESSION['data'][$i]['FieldTwo'] . '</td>
                    <td>' . $_SESSION['data'][$i]['FieldThree'] . '</td>
                </tr>
            ';
         }
     ?>
     </tbody>
</table>

<script>
    $('#show-data').click(function() {
        $.ajax({
            url: 'php/functions.php',
            data: { "function": "showData" },
            complete: function (response) {
                location.reload();
            },
            error: function() {
                console.log("Error");
            }
        });
        return false;
    });
</script>

functions.php

<?php
    session_start();

    $DB_host = "localhost";
    $DB_user = "me";
    $DB_pass = "password";
    $DB_name = "data";

    $DB_con = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
    $DB_con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

    if ($_GET["function"] == "showData")
    {
        showData();
    }

    function showData()
    {
        $stmt = $DB_con->prepare("SELECT * FROM data");
        $stmt->execute();

        $data = array();
        if ($stmt->execute())
        {
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
            {
                $data[] = $row;
                $_SESSION['data'] = $data;
            }
        }
    }
?>
Mr.Smithyyy
  • 1,829
  • 9
  • 38
  • 77
  • 1
    Shouldn't it be `showUsers` instead of `showData` in your ajax call? – Mikey Feb 16 '16 at 16:36
  • Run session_start() at the beggining of functions.php, and in every file which you will use SESSION variables – Phiter Feb 16 '16 at 16:36
  • @Mikey no I changed my example from my actual code just because it would have been too much to copy/paste, but I must have forgot to edit that part. – Mr.Smithyyy Feb 16 '16 at 16:38
  • @PhiterFernandes Just tried it, still just reloads page with no data. – Mr.Smithyyy Feb 16 '16 at 16:40
  • `session_start()` should also be placed at the beginning of index.php – Mikey Feb 16 '16 at 16:41
  • 1
    How does that duplicate question help me? – Mr.Smithyyy Feb 16 '16 at 16:43
  • 1
    That duplicate question is unrelated to the question. – Mikey Feb 16 '16 at 16:44
  • Yes it is unrelated and now my question is closed. I don't get how he is linking the two? – Mr.Smithyyy Feb 16 '16 at 16:45
  • Hey this is not a duplicate. Please don't do like this. – Mr. Engineer Feb 16 '16 at 16:48
  • First thing, I'd do when attempting to debug this would be to [turn on errors](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) at the beginning to see if anything magically shows up. Your ajax script is relatively simple, so then I'd print out the session at the end of the script using `print_r($_SESSION['data'])` and go directly to the script via the browser's URL to see if anything outputs. – Mikey Feb 16 '16 at 16:53
  • just a suggestion : put this line `$_SESSION['data'] = $data;` out of the while loop. – Mr. Engineer Feb 16 '16 at 17:20

0 Answers0