Proper query syntax

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
taldos
Forum Commoner
Posts: 39
Joined: Mon Aug 23, 2004 8:47 am
Location: Philadelphia

Proper query syntax

Post by taldos »

Could someone please tell me the difference between teh following two statements.

Code: Select all

$my_sql = "SELECT * FROM `my_leads` WHERE (`read`='0' AND `owner_id`='".$my_id."')";

$my_sql = "SELECT * FROM my_leads WHERE (read=0 AND owner_id=".$my_id.")";
I have used syntax similar to the second statement for a while now and it has been working. All of a suddden today, everything is no longer working and I think this is the problem. Does it have anything to do with the way the tables are set up?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

mysql allows you to use the backticks so that you can use reserved words, operators and keywords for table and field names.

ex:

a field name this-field would not be valid because of the minus (-) sign.

so if you tried this:

Code: Select all

select * from myTable where this-field = 'joe'
the query would bomb out...whereas

Code: Select all

select * from `myTable where `this-field` = 'joe'
would work.
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Re: Proper query syntax

Post by harrisonad »

As burrito said, they are the same, Yet I found these
taldos wrote:`read`='0'
read=0
string to integer is not the same though
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mysql does implicit conversion on numeric strings to integers/floats and back..
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

oic

That's new to me. Thanks
taldos
Forum Commoner
Posts: 39
Joined: Mon Aug 23, 2004 8:47 am
Location: Philadelphia

Post by taldos »

Oh ok. in other words, "read" was a reserved word which I need to use the escape character for.

Thanks

Cheers :D
Post Reply