Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm attempting to pass an AngularJS variable to a mysql query. I can't figure out why it won't work. Here is the code:

$test = "{{tvShows[whatShow].tvdb_id}}";

$query = "SELECT * FROM tv_seasons WHERE EXISTS (SELECT tvdb_id FROM tv_episodes WHERE tv_episodes.tvdb_id=tv_seasons.tvdb_id AND tv_episodes.season=tv_seasons.season) AND tvdb_id = '";
$query .= $test;
$query .= "' ORDER BY CAST('episode' AS UNSIGNED INTEGER)";

$results = mysql_query($query);

while($row = mysql_fetch_array($results)){      
    print_r($row);
    echo "<br>";
}

$test echos properly and the query looks correct and when pasted into phpMyAdmin works as expected but when I try to do this it wont work for some reason. Any thoughts?

share|improve this question
    
AngularJS variable in PHP? Doesn't make sense. –  camden_kid 20 hours ago
    
i'm learning AngularJS and i'm currently using a mysql database. In order to use said db I need to use php and in order to perform the query i need to i need to get the value of an AngularJS variable and pass it to my query in php. confusing.. i know. –  jkingaround 19 hours ago
    
Have you head of POST requests? or GET? –  baldrs 19 hours ago
    
It wouldn't help me as it's not in the url.. –  jkingaround 19 hours ago
add comment

1 Answer

Post it to server like that(angular requests docs):

$http.post(your_script_uri, {your_var_key: your_var}).
success(function(data, status, headers, config) {
  // this callback will be called asynchronously
  // when the response is available
}).
error(function(data, status, headers, config) {
  // called asynchronously if an error occurs
  // or server returns response with an error status.
});;

Then on server side:

$_POST['your_var_key'] // use POST superglobal to access data you have posted

$_POST docs

Your code, modified:

$query = "SELECT * FROM tv_seasons WHERE EXISTS (SELECT tvdb_id FROM tv_episodes WHERE tv_episodes.tvdb_id=tv_seasons.tvdb_id AND tv_episodes.season=tv_seasons.season) AND tvdb_id = '";
$query .= mysql_real_escape_string($_POST['tvdb_id']);
$query .= "' ORDER BY CAST('episode' AS UNSIGNED INTEGER)";

$results = mysql_query($query);

while($row = mysql_fetch_array($results)){      
  print_r($row);
  echo "<br>";
}

PS: stop using mysql extension. It is deprecated in favor of mysqli extension.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.