0

Currently, I made script, which after onclick event,sending question to the database and showing data in console.log( from array ). This all works correctly, but.. I want to show data from array in the different position in my code. When I try to use DataType 'json' and then show some data, then it display in my console.log nothing. So, my question is: How to fix problem with displaying data? Is it a good idea as you see?

Below you see my current code:

    $(document).ready(function(){
    $(".profile").click(function(){

        var id = $(this).data('id');

        //console.log(id);
        $.ajax({
            method: "GET",
            url: "../functions/getDataFromDB.php",
            dataType: "text",
            data: {id:id},

            success: function(data){

                    console.log(data);
            }

        });

    });
});

:

    public function GetPlayer($id){
    $id = $_GET['id'];

    $query = "SELECT name,surname FROM zawodnik WHERE id='".$id."'";
    $result = $this->db->query($query);
    if ($result->num_rows>0) {
        while($row = $result->fetch_assoc()){
            $this->PlayerInfo[] = $row;
        }
        return $this->PlayerInfo;


    }else {
        return false;
    }
}

:

    $info = array();

    $id = $_GET['id'];

    $vv = new AddService();

    foreach($vv->GetPlayer($id) as $data){
        $info[0] = $data['name'];
        $info[1] = $data['surname'];
    }

    echo json_encode($info);

2 Answers2

0

Try this:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<button class="profile" data-id="1">Click</button>
<script
  src="https://code.jquery.com/jquery-3.3.1.min.js"
  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  crossorigin="anonymous"></script>
<script>
$(document).ready(function(){

    $(".profile").click(function(){
            var id = $(this).data('id');

            console.log(id);
            $.ajax({
                    method: "GET",
                    url: "../functions/getDataFromDB.php",
                    dataType: "json",
                    data: {id:id},
                    success: function(data){
                        console.log(data);
                        $.each(data, function(idx, item) {
                            console.log(item.surname);
                        });
                    }
            });
    });

});
</script>
</body>
</html>

PHP side:

    <?php
    class AddService {

       public function GetPlayer($id) {
            if (filter_var($id, FILTER_VALIDATE_INT) === false) {
                return false;
            }

            $query = "SELECT name, surname FROM zawodnik WHERE id={$id}";
            $result = $this->db->query($query);

        if ($result->num_rows <= 0) {
                return false;
            }

          // assumming you are using mysqli
          // return json_encode($result->fetch_all(MYSQLI_ASSOC));
          // or
          WHILE ($row = $result->fetch_assoc()) {
            $data[] = $row;
          }
          return json_encode($data);
        }
    }

    if (isset($_GET['id'])) {

        $id = $_GET['id'];

        $vv = new AddService();
            // you don't need foreach loop to call the method
            // otherwise, you are duplicating your results
        echo $vv->GetPlayer($id);
    }
Michael Eugene Yuen
  • 2,470
  • 2
  • 17
  • 19
  • I changed my code as you wished, but... It still doesn't work. It shows me nothing again ( in console.log ). It doesn't even shows a new line. –  Mar 11 '18 at 10:35
  • Enable error display in your php script. php error will be shown on your console log: https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display. I have tested the script before sharing – Michael Eugene Yuen Mar 11 '18 at 10:37
  • Have you defined your db connection? Have your tested your class/method directly without ajax? – Michael Eugene Yuen Mar 11 '18 at 10:45
  • Before changing GetPlayer function, the data in console.log section was displayed correctly. –  Mar 11 '18 at 10:49
  • Are you using pdo or mysqli extension? // assumming you are using mysqli <=== – Michael Eugene Yuen Mar 11 '18 at 10:50
  • I using mysqli extension. –  Mar 11 '18 at 10:51
  • When I use a piece of code like this: while($row = $result->fetch_all()){ $this->PlayerInfo[] = $row; } return json_encode($this->PlayerInfo); and then use dataType: 'text' console.log show me an array with data. –  Mar 11 '18 at 10:57
  • Then use yours. Most likely you are using php version 5.3 or earlier. https://stackoverflow.com/questions/6694437/mysqli-fetch-all-not-a-valid-function – Michael Eugene Yuen Mar 11 '18 at 10:59
  • I use a php 7.2.0 at the moment. When I use my code, It shows me like this: [[["77","test","test","1","1","test","men"]]]. –  Mar 11 '18 at 11:06
  • When I change fetch_all to fetch_assoc isn't much difference. It shows then [{"id":"77","name":"test","surname":"test","gender":"1","school":"1","name":"test","gender":"men"}] –  Mar 11 '18 at 11:10
  • Isn't much different is big different in programming. Now you are getting your json. And I have given you an example on how to get individual value from the object. I can't offer much help further. – Michael Eugene Yuen Mar 11 '18 at 12:19
  • I still don't know, what to do. After changing DataType from "text' to "json" then it doesn't show me nothing. –  Mar 11 '18 at 13:05
  • It must be a 'json' dataType? Or 'text' could be okey? –  Mar 11 '18 at 13:14
  • If you use dataType: 'text' your will need this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse – Michael Eugene Yuen Mar 11 '18 at 13:30
  • I did this, and I received error: Uncaught SyntaxError: Unexpected token in JSON. –  Mar 11 '18 at 14:34
0

I think it would be better to change the line fetch_all in mysqli to rm -rf. That information in the DB is all obsolete, or completely not true.