php - When to use single quotes, double quotes, and backticks in MySQL -


I am trying to figure out the best way to write the questions. I understand the importance of being consistent so far, I have randomly used single quotes, double quotes and backticks without any real idea.

Example:

  $ query = 'INSERT in the table (id, col1, cola2) value (tap, val 1, val 2)'; In addition, in the example above, suppose that the  "table," "col [n]" and "val [n]"  can be variable is. / P> 

What is the standard for this? What do you do

I have been reading answers to similar questions for about 20 minutes but it seems that there is no definitive answer to this question.

Double quotes in string values ​​such as VALUES () list are supported by MySQL for string values, but single quotes are accepted more widely by other RDBMS , So there is a good habit of using single quotes rather than double.

MySQL is also single-quoted in the form of a string like date and DATETIME , such as '2001-01-01 00 : 00: 00 '. For more details, especially using Hyphen - , consult the document for more information as a segment delimiter in the debug string.

By using my example, I use a string of PHP strings at double values ​​ 'val1', 'val2' . NULL is a MySQL keyword, and is a special (non) value, and so it is uncompressed.

None of these table or column identifier is the reserved word quote, but I have cited them with some kind of backlit (and later on it ...).

The RDBMS (e.g., NOW () should not be quoted in MySQL), although their arguments are subject to the same string or identifier, which are already mentioned in the rules Quote.

 backtick (`) table and column ───────┬─────┬─ ─┬──┬──┬────┬──┬────┬ ──┬────┬──┬───────┐ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ $ query = "` table` (`id`,` call1`) , Include in `Cola 2`,` Date`, `Updated '] Values ​​(Zero,' Val 1 ',' Val 2 ',' 2001-01-01 ', Now ()) "; ^ Aiyar Afyar Haiffer ↑ ^ ↑ ↑ ↑ ↑ ↑ Primary Community Dept. Awarded Character ─────┴┴┴┘ │ │ │ │ │ │ │││││ Single-quoted (') Strings ────── ── ───┴────┴──┴──── │ │ │││││ single-quoted (') DATE ───────────────── ──────────┴──────── ──┘ │││││ untold function ───────────────────── ─────────────────── ─┴┴┴┴┘ 

convertible interpolation

does not change the quote patterns of variables, even if you intend to launch the variable directly into the string, then double it into PHP - Be sure to use the variable in order to properly use it in SQL The same thing with some variables replacement // here, a variable table name $ table bracket-quoted, and variable // values ​​// single quoted $ Query = "INSERT values ​​(zero, '$ val1' , $ $ ` $ table` (`id`,` call1`, `call2 `,` Date``, Val2 '' , '$ date' ) ";

Ready statement

While working with a prepared statement, consult the document to determine whether the placeholder of the statement should be cited or not. The most popular APIs available in PHP, PDO and MySQLi, unquoted placeholders in other languages, expect the APIs of the most prepared statement:

  // PDO example designated Include in the parameter, without quoting $ query = "table`" (`id`,` call1`, `call2`,` date`). VALUES (: id,: col1 ,: col2 ,: date) "; // MySQLi with example? VALUES (?,?,?,?) ";   in the parameters, without quoting $ query =" table` "(` id`, `call1`,` call2`, ​​`date`} Pre-

characters retrieving the quoted quoted in identifiers:

, you do not need quote (bitak) identifier to use the following character set:

ASCII: [0- 9, az, AZ $ _] (Original Latin letter, number 0-9, dollar, underscore)

For example, with white space, for example Characters beyond the set as H or column identifier, but then you should quote (bracketic) them.


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -