1

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('something wrong');
        })
        })
        }

Just a request for data. But I'm confused with my php code, because I'm 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}

2
  • 1
    First, try to call directly through the browser the downloadMessage.php, and did you check the return of query using a mysql client? Commented Jan 14, 2018 at 13:27
  • 1
    mysqli_query does not return result you expect. fetch_ functions do it. Commented Jan 14, 2018 at 13:38

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much :) of course, I know about validations, just wanted to get all, now I can code this :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.