Newbie needs help with quotes!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
katkat
Forum Newbie
Posts: 23
Joined: Tue Mar 03, 2009 9:50 pm

Newbie needs help with quotes!!

Post 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.'";
      
 
 
 
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Re: Newbie needs help with quotes!!

Post 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.
katkat
Forum Newbie
Posts: 23
Joined: Tue Mar 03, 2009 9:50 pm

Re: Newbie needs help with quotes!!

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Newbie needs help with quotes!!

Post 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.
katkat
Forum Newbie
Posts: 23
Joined: Tue Mar 03, 2009 9:50 pm

Re: Newbie needs help with quotes!!

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Newbie needs help with quotes!!

Post 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
}
katkat
Forum Newbie
Posts: 23
Joined: Tue Mar 03, 2009 9:50 pm

Re: Newbie needs help with quotes!!

Post by katkat »

thanks a million!
I really appreciate it. I've got lots to learn.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Newbie needs help with quotes!!

Post 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.
Post Reply