I have a database and I want to count the number of times a state shows up in it. I wanted to use this number later for calculations. However, when I try to do something like this (which correctly gives me '1' when I execute it in phpmyadmin):

(on controller) $sql = "SELECT count(state) FROM survey where state=\'New York\'";
(on view page) <?php echo $sql?>

I get this:

Notice: Undefined variable: sql in /home/ubuntu/workspace/[NAME]/views/results_form.php on line 3

Removing the backslashes made no difference. The structure of my database is like this (gender is a boolean):

  • id - state - gender
  • 1 - Florida - 1
  • 2 - New York - 0

I think that the query is not even able to reach the database at all, if that makes sense. I imported tables from pset7 into phpmyadmin and tried to use one of the queries from pset7, and I got the same error. I'm not sure how to fix it if that's the case.


This $sql = "SELECT count(state) FROM survey where state=\'New York\'"; makes $sql a string literal. It's difficult to determine, from two lines of code, why the view page is giving an "undefined", but it will never give the result of the query unless it gets "sent" to the database as with query($sql).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .