1
我想在php中編寫select查詢以從前端使用變量從數據庫中獲取數據。換句話說,我需要做的是這樣的:從用戶 選擇電子郵件其中token =「$令牌」 我現在有,我有代碼的帖子我的變量到後端:如何在php中使用角度變量從數據庫中選擇數據
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/PHP/test.php的,我得到的通知:試圖在/應用程序以獲取非對象的屬性/ XAMPP /xamppfiles/htdocs/php/test.php在第8行,這意味着我猜$標記是空的,但是當我在console.log(響應)角度它顯示我在c中的令牌我認爲這意味着來自前端的數據傳遞到後端。或者我的理解錯了?
會非常感激的幫助!
這個問題是通過使用Cookie,而不是試圖從本地存儲數據的解決。 –