我正在使用angular的$ http.get函数调用PHP脚本文件来检索数据
var $promise = $http.get("../php-bin/example.php",obj).success(function(data) {
console.log(data);
});
php应该只是从mysql数据库中获取数据,以找出将数据导入我的应用程序的方法
$user = json_decode(file_get_contents('php://input'));
$email = $user->email;
$pass = $user->pass;
$con = //a mysql_connection;
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$validateEmail = "SELECT `Email` FROM `newUsers` WHERE `Email` = '$email' ";
if ($result = mysqli_query($con,$validateEmail)) {
if ($result->num_rows == 1){
$date = '2014-08-13';
//$sql = "INSERT INTO newUsers (Email, CreationDate, UserRef, Type, id) VALUES ('$email','$date','$email','Host','$ssid')";
$sql = "SELECT `email` FROM `newUsers` WHERE `hashpass` = '$pass' ";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($result);
return $row;
//I never receive this data in the angular app.
}
}
mysqli_close($con);
?>
有人可以指出我这样做的正确方法。
答案 0 :(得分:4)
我看到你有return
语句而不是echo
语句。 PHP输出中不打印return
语句。 A return
statement outside a function has only sense when you include the file:
$returned = include('the_file_you_showed_here.php');
//you will hold the row here
return
语句kills the current script返回到包含者脚本(如果有),但不向输出发送任何值(这是die
/ {的目的{1}})。你应该:
exit
更改为return
。echo
指令或非php内容之前发送json数据,请记得发送header('Content-type: application/json')
。echo
。答案 1 :(得分:3)
确保你回应你的结果,回声结果传递到你的变量
例如,如果您从范围传递一个varibale,那么
function($scope,$http){
$scope.obj= "your variable";
$reuslt = http.get('yourlocation/yourfile.php?val='+obj');
你的php脚本
<?
$value = $_GET['val']
//not your variables have been passed in and you can use it to do your custom function wich ever way you like
and echo your result after wards
?>
希望这会有所帮助
答案 2 :(得分:1)
你不能这样调用相对路径。该文件必须存在于公共目录或更低的位置。例如,如果您的所有文件都在/ home / yourname / public中,则需要调用/php-bin/example.php
,其中php-bin位于公共目录(/ home / yourname / public / php-)中bin)中。