如何在php中使用角度变量从db中选择数据

时间:2016-10-08 13:58:13

标签: php mysql angularjs

我想在php中编写select查询,以便从前端使用变量从数据库中获取数据。换句话说,我需要做这样的事情:     从用户处选择电子邮件WHERE token =' $ token' 我目前拥有的,我有代码将我的变量发布到后端:

app.controller('customersCtrl', function($scope, $http,$templateCache) {
    $scope.subscribe = function (gameId){
        $scope.codeStatus = "";
        $http({
            url: "php/test.php",
            method: "POST",
            data: {
                userId: JSON.parse(localStorage.getItem('loggedUserInfo')),
                gameId: gameId,
                token:JSON.parse(localStorage.getItem('token'))
            },
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            cache: $templateCache
        }).success(function(response) {
            $scope.codeStatus = response.data; 
            console.log(response);
        });

    };

});

这是我的PHP代码:

<?php
$lnk = mysql_connect('localhost', 'root', '')
    or die ('Not connected : ' . mysql_error());

mysql_select_db('pitch', $lnk) or die ('Can\'t use db : ' . mysql_error());

$data = json_decode(file_get_contents("php://input"));
$token=$data->token;   

$dump1 = mysql_query("SELECT email FROM users where token = '$token' ");

if(!$dump1) exit("Error - ".mysql_error()); 
$getList = array(); 
$outp = "";
while ($row = mysql_fetch_assoc($dump1)) { 
    $relations = array();
    $getList[] = $row;
}
$output = json_encode(array('users' => $getList));

echo $output; 
?>

对我来说最奇怪的是,当我尝试去url localhost / php / test.php时,我得到一个通知:尝试在/ Applications / XAMPP / xamppfiles / htdocs / php / test中获取非对象的属性第8行的.php,这意味着我猜$ token是空的,但是当我在console中的console.log(响应)时它会在控制台中显示我的令牌,我认为这意味着来自前端的数据被传递到后端。或者我的理解是错的?

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

首先尝试var_dump($ _ POST),或尝试在test.php上使用var_dump($ data)