I want to write select query in php to get data from database using variable from front-end. In other words I need to do something like this: SELECT email FROM users WHERE token = '$token' What I currently have, I have code which posts my variables to back end:
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);
});
};
});
And here is my php code:
<?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;
?>
The weirdest thing for me is that when I try to go to url localhost/php/test.php I get an Notice: Trying to get property of non-object in /Applications/XAMPP/xamppfiles/htdocs/php/test.php on line 8, which means I guess that $token is empty, but when I console.log(response) in angular it shows me my token in console which I think means that data from front-end was passed to back-end. Or my understanding is wrong?
Will be really thankful for help!