0

I'm trying to do a simple http get request but it doesn't return what i expected.

Here is the code

.js :

$http.get("database.php").success(function(data, status)
{
    alert(data);
    $scope.data = data;
}
).error(function(data, status)
{
    $scope.untruc = "Error";
});

.php :

<?php
header('Content-type: application/json');

$conn = // my connection

// Check connection
if ($conn->connect_error)
    echo "Connection failed";
else
{
    $sql = "SELECT id FROM user";
    $result = $conn->query($sql);
    echo json_encode($result);
}

?>

and the ouput is :

data : <?php header('Content-type: application/json'); $conn = // my connection; // Check connection    if ($conn->connect_error) echo "Connection failed"; else { $sql = "SELECT id FROM user"; $result = $conn->query($sql); echo json_encode($result); } ?>

literaly the code...

I'm expecting it to get the $result. Do you know how i could do that ?

3
  • 2
    Most likely a misconfigured webserver that does not pass the PHP files through the PHP interpreter Commented Jul 6, 2017 at 8:33
  • 2
    Is PHP installed on your server? For some reason your .php file isn't being handled as it should and returns the PHP code in plain text. Commented Jul 6, 2017 at 8:34
  • I just realised i wasn't testing it on a server but on local... Thank you Commented Jul 6, 2017 at 8:35

1 Answer 1

1

Please check you php code once. And try to implement this code

<?php
header('Content-type: application/json');

$conn = new mysqli('localhost', 'root', 'password', 'database');

// Check connection
if ($conn->connect_error)
    echo "Connection failed";
else
{
    $sql = "SELECT * FROM users";
    $result = $conn->query($sql);
    $myresult = [];
    $i =0;
    while($row = $result->fetch_array()) {
		$myresult[$i] = $row;
		$i++;
	}
    
    //echo "<pre>";
    echo json_encode($myresult);
}

?>

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

Comments

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.