-2

I have seen different ways on how to use it but I don't understand the why...

say a simple SQL..

$q = "UPDATE table SET
col1 = '$var1',
col2 = '$var2'
 ";

So that is one way...

$q = "UPDATE table SET
`col1` = $var1,
`col2` = $var2
 ";

This does the same, but why the use of `?

and then:

$q = "UPDATE table SET
'col1' = $var1,
'col2' = $var2
 ";

So what is the correct way to use it, when to use it, and why... then I have seen this:

$q = "UPDATE table SET
col1 = ".$var1.",
col2 = ".$var2";

Thank you for taking the time.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Tanker
  • 1,178
  • 3
  • 16
  • 49

2 Answers2

0

The backtick ` is for reserved words or column names, while a normal single quote ' is to encapsulate strings.

You can read the detailed info here: https://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

Edit: As pointed out, this is for MySQL only. Different database engines use different methods to encapsulate tables, solumns or reserved words.

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
0

what happens when your column is called "date" ? Mysql will not know if you have used a function wrong or if you meant a column called date

It should be used often to make mysql aware your using a column and not let it do the guess work.

The second question, there is no difference. But you shouldnt build query's like that, instead look at prepared statements

http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php

exussum
  • 18,275
  • 8
  • 32
  • 65