Php and angularjs - getting data

I am creating simple request for GET a "message" with title etc from MySQL server.

So, I've got something like this in my angularJS:

        $http({
            url: 'http://localhost/webpack/downloadMessage.php',
            method: 'GET',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).then(function(response){
            console.log("CHECKED");
            console.log(response.data);
        }, function(response) {
            alert('sommething wrong');
        })
        })
        }

Just a request for data. But im confued with my php code, cause im a beginner, can you help me what's wrong? I want just whole table where section = 3.

<?php 

header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');

if(!isset($_POST)) die();

session_start();

$response = [];

$con = mysqli_connect('localhost', 'root', '', 'projekt');

$query = "SELECT * FROM messages WHERE section='3'";

$result = mysqli_query($con, $query);

echo json_encode($result);

It's without errors, but in my console i've got this:

CHECKED {current_field: null, field_count: null, lengths: null, num_rows: null, type: null}

1 answer

  • answered 2018-01-14 13:44 u_mulder

    According to a manual result of mysqli_query is either a mysql_result or false if query fails.

    To get data from mysqli_result you need to fetch it, for example with fetch_assoc:

    $result = mysqli_query($con, $query);
    $row = mysqli_fetch_assoc($result);
    
    echo json_encode($row);
    

    Also, there can be cases when you have no results ($row will be empty) or your query fails ($result will be false). These cases should be checked in your script and appropriate response should be returned.