Jump to content

PHP Parse Error


deslyxia

Recommended Posts

I am getting the following code when executing a query:

 

Parse error: syntax error, unexpected T_VARIABLE

 

 

Below is the query it came from.

 

//query db and build name array
$qry="SELECT * FROM Patient WHERE LName LIKE "$q"";
$result=mysql_query($qry);

 

I know that the issue is coming from the double quotes around $q. If I switch them to single quotes I get no error... but I also get no results. I have looked up the difference between single and double quotes and I think i have it right.

 

Single quotes use exactly what is in them  ex.  echo '$q';  would be $q

Double quotes will evaluate the variable and use that 

ex.  $q = 'php';

echo "$q";  would be php

 

So this has me kind of lost as to what the issue is in my query.

Link to comment
Share on other sites

The correct php and mysql syntax would be -

 

$qry="SELECT * FROM Patient WHERE LName LIKE '$q'";

 

Using LIKE without any wildcard characters in the pattern is in most cases the same as using an equal comparison -

 

$qry="SELECT * FROM Patient WHERE LName = '$q'";

 

What LName value are you trying to match and what value are you putting into the $q variable?

Link to comment
Share on other sites

In my Table I have hundreds of LName. $q comes from a user input field on the main page. The purpose here is that every time a user types a letter in the field this query selects all rows from my table that contain the user input string.

Link to comment
Share on other sites

I'll assume you mean you want to find names that start with the entered string. You would use a trailing % wildcard to match everything that starts with the entered string -

 

$qry="SELECT * FROM Patient WHERE LName LIKE '$q%'";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.