Jump to content

Why do i not get any results returned from this code?


Recommended Posts

Why do i get no results returned from the below form?

Ive tested the string;

SELECT phone FROM mytablename WHERE sg ='YES' ORDER BY companyName LIMIT 3

and the sql is fine in mysql admin, and returns the expected results. However in practice I seem to be having difficulties. Is my syntax out of place.


All i get returned are the two words "Phone numbers: "


Im trying with difficulty to learn php! please any pointers?



<!-- html only form.html-->


<form method="get" action="phponlyform.php">

<input type="text" name="postcode" size="2" maxlength="2"/>

<input type="submit" name="Submit"/>



<!-- phponlyform.php-->




mysql_select_db("mydatabase") or die("Unable to select database");

$code = $_GET['postcode'];

$result = mysql_query("SELECT phone FROM mytablename WHERE '$code' ='YES' ORDER BY companyName LIMIT 3")

or die(mysql_error());


$row = mysql_fetch_array( $result );


echo "Phone Numbers: ".$row['phone'];


<!-- eg. column name is SG and he value is YES -->


Link to comment
Share on other sites

Your query is wrong, or your database design is fundamentally flawed. This: WHERE '$code' ='YES' will never evaluate to TRUE, unless the value coming from the 'postcode' form field is 'YES', and then the query would return all records in the database. This is because in that piece of code, you are checking whether the value of the $code variable is equal to the literal string 'YES'.

Link to comment
Share on other sites

the actual database would be somthing like this;


phone number / company name / SG / EN / CM / SW / NE / SE


0800 089098 / Company A / YES / NO / YES / YES / YES / YES

0800 111111 / Company B / NO / YES / NO / YES / NO / YES

0800 222222 / Company C / YES / NO / NO / NO / NO / NO

0800 333333 / Company D / NO / NO / NO / NO / NO / NO

0800 444444 / Company E / YES / YES / NO / NO / YES / YES


I have it like this, as the companies will subscribe to different postcode areas.

This is why for example in my html form the user typed in EN,


I would want the phone numbers of company b and company e displayed.



Im i right in thinking that I would have to change the values of YES and NO to another word such as "subscribes" and "no subscription"?

Link to comment
Share on other sites

No, you need to change '$code' to `$code`. Quotes are for string values, backticks are for database table and field names.


EDIT: Something still seems strange about this, but I can't put my finger on it at the moment for some reason. . .

Link to comment
Share on other sites

Thankyou for getting back to me.


I did as you said, and im perhaps another step forward, as it seems as if mysql query is being processes.  I do however now get a response of ;


Unknown column 'yes' in 'where clause'


Does this mean that for some reason that the value for `$code` is being totally ignored, leaving 'yes' to be the column name opposed to being a value?


heres my browser url

eg.  If i type EN into my html form;




Many thanks

Link to comment
Share on other sites

How do you figure it should be $_POST, when the form method is clearly 'get'?


Your right. I ignored the method='get'.


Try and echo out $code and see what result you get when you submit the form. Also echo $result; to see if the query is fine. If your $code is left empty in the form then you will get an error like "Unknown column 'yes' in 'where clause'", you will also get that error if the column name written in the form is not in the database. It is better to use a validation statement in the code so it can't be left empty or has to equal to one of the column names.

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.