I'm working on an SQL query that selects results based on a User ID number foreign key, and displays the result if there are any matching entries in the correlated table. When I try to use a PHP variable in my WHERE clause, it seems like the SQL query doesn't recognize a value at all, even though if I print the variable, there is a value there.
I've attempted changing the query with the "'. $variable .'"
and ' . $variable . '
, but I still get the same result. Here is my code:
$usernum = intval($_SESSION['usernum']);
$query = "SELECT * FROM dinosaurs WHERE UserNum = '$usernum'";
When I enter the variable manually (for example "SELECT * FROM dinosaurs WHERE UserNum = '6'";
it works, but when I attempt to run this query I get the following result:
SELECT * FROM dinosaurs WHERE UserNum = ''
returned 0 records.
It looks like the variable is just showing a blank value in the SQL statement? How would I get it to recognize the variable's value?
EDIT: SOLUTION I figured it out! It was a dumb error where I was declaring the variable inside of a different function instead of globally so I moved the declaration to the correct function like this:
// print_r($json);
$usernum = intval($_SESSION['usernum']);
}
So that just goes to show that I should pay more attention to my own code :)