-2

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?

4
  • AngularJS variable in PHP? Doesn't make sense. Commented May 17, 2014 at 11:01
  • 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. Commented May 17, 2014 at 11:05
  • Have you head of POST requests? or GET? Commented May 17, 2014 at 11:07
  • It wouldn't help me as it's not in the url.. Commented May 17, 2014 at 11:08

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.