Page 1 of 1

Newbie needs help with quotes!!

Posted: Sun Mar 15, 2009 5:44 pm
by katkat
Newbie!!
I need help with the quotes arround the field names in the WHERE conditions.
The variables are passed in a function and contain string info. I am unable to get this to execute. Any help?

Code: Select all

 
// should it be this: 
 
$query = "SELECT * FROM runners WHERE firstname=$firstname AND lastname=$lastname AND passw=$pw";
 
// or should it be this:
 
$query = "SELECT * FROM runners WHERE firstname='$firstname' AND lastname='$lastname' AND passw='$pw'";
 
// or this:
 
      $query = 'SELECT * FROM runners WHERE 'firstname' = "' .$firstname. '" AND 'lastname' = "' .$lastname. '" AND 'passw' = "' .$pw.'";
      
 
 
 

Re: Newbie needs help with quotes!!

Posted: Sun Mar 15, 2009 7:59 pm
by socket1
The first two should work unless you have spaces in any of your variables, it would be safest to use the middle one.

Re: Newbie needs help with quotes!!

Posted: Sun Mar 15, 2009 8:13 pm
by katkat
There are spaces.

These are names that come from a entry form. The entry form input line has a designated size of 50. When I DESCRIBE the file in the console, the type is designated as char(30) which I am assuming means 30 characters. Do I need to make sure everything matches in size?

Re: Newbie needs help with quotes!!

Posted: Sun Mar 15, 2009 8:41 pm
by requinix
The first one will not work. Second and third may.

A CHAR(30) means you can have up to 30 characters in that field. If you provide more they will be cut off - warnings (which are hard to get to in PHP) but no errors.
If you don't want them truncated them you should check that the data is short enough; if not you can print a message or whatever you want to do.

Re: Newbie needs help with quotes!!

Posted: Sun Mar 15, 2009 9:02 pm
by katkat
I got the 2nd one to work. Thanks so much for that! But now I have another problem:

If a user enters a name that doesn't exist, what is the test?

I was using a:

if ($result) { then display the record } else display an error message.

But what is happening is the name that the user enters (even though it is not in the file) is displayed and no error message.

Re: Newbie needs help with quotes!!

Posted: Sun Mar 15, 2009 11:00 pm
by califdon
katkat wrote:I got the 2nd one to work. Thanks so much for that! But now I have another problem:

If a user enters a name that doesn't exist, what is the test?

I was using a:

if ($result) { then display the record } else display an error message.

But what is happening is the name that the user enters (even though it is not in the file) is displayed and no error message.
That's because returning an empty result set is not an error, even though it may not be what you wanted. There are several ways to handle this. Instead of testing $result, you can test

Code: Select all

if(mysql_num_rows($result) > 0) {
    // display record
} else {
   // display error message
}

Re: Newbie needs help with quotes!!

Posted: Mon Mar 16, 2009 9:32 am
by katkat
thanks a million!
I really appreciate it. I've got lots to learn.

Re: Newbie needs help with quotes!!

Posted: Mon Mar 16, 2009 11:01 am
by califdon
katkat wrote:thanks a million!
I really appreciate it. I've got lots to learn.
So have I. This isn't simple stuff. Good luck. Have fun.