Using PHP 5.6 and PostgreSQL
I'm relatively new to PHP. I'm sorting results on a page using a query string, like so:
if (isset($_GET['sort'])) {
$criteria = $_GET['sort'];
$result = get_all_sorted_restaurants_with_limit($limit, $criteria);
} else {
$result = get_all_restaurants_with_limit($limit);
}
where 'sort' is the url query string value. From doing a var_dump, I can see its correctly grabbing a string value.
Next, if the value is set, it calls the following method in my model:
function get_all_sorted_restaurants_with_limit($limit, $sort) {
$conn = open_database_connection();
$result = pg_query($conn, "SELECT * FROM restaurant r WHERE r.Type =
$sort LIMIT $limit;");
close_database_connection();
return $result;
}
Here's where things get wonky: When I try doing it with $sort, in the get_all_sorted_restaurants_with_limit method, it returns nothing. However, if I set it myself (i.e. to 'sports bar', like so:
$result = pg_query($conn, "SELECT * FROM restaurant r WHERE r.Type = 'sports bar' LIMIT $limit;");
it works. The var dump shows that this is the exact same string that's being grabbed from the query url.
How would I fix this? Do I need to cast it, or grab a strval() (I've tried, doesn't work.... ).