Php mysql select single value from table

By: memb3r Date: 21.06.2017

I am trying to learn the best way to write queries. I also understand the importance of being consistent. Until now, I have randomly used single quotes, double quotes, and backticks without any real thought.

Also, in the above example, consider that "table," "col[n]," and "val[n]" may be variables. I've been reading answers to similar questions on here for about 20 minutes, but it seems like there is no definitive answer to this question. Backticks are to be used for table and column identifiers, but are only necessary when the identifier is a MySQL reserved keywordor when the identifier contains whitespace characters or characters beyond a limited set see below It is often recommended to avoid using reserved keywords as column or table identifiers when possible, avoiding the quoting issue.

Single quotes should be used for string values like in the VALUES list. Double quotes are supported by MySQL for string values as well, but single quotes are more widely accepted by other RDBMS, so it is a good habit to use single quotes instead of double. MySQL also expects DATE and DATETIME literal values to be single-quoted as strings like ' Consult the Date and Time Literals documentation for more details, in particular alternatives to using the hyphen - as a segment delimiter in date strings.

So using your example, I would double-quote the PHP string and use single quotes on the values 'val1', 'val2'. NULL is a MySQL keyword, and a special non -value, and is therefore unquoted. None of these table or column identifiers are reserved words or make use of characters requiring quoting, but I've quoted them anyway with backticks more on this later Functions native to the RDBMS for example, NOW in MySQL should not be quoted, although their arguments are subject to the same string or identifier quoting rules already mentioned.

The quoting patterns for variables do not change, although if you intend to interpolate the variables directly in a string, it must be double-quoted in PHP. Just make sure that you have properly escaped the variables for use in SQL. It is recommended to use an API supporting prepared statements instead, as protection against SQL injection.

When working with prepared statements, consult the documentation to determine whether or not the statement's placeholders must be quoted. The most popular APIs available in PHP, PDO and MySQLi, expect unquoted placeholders, as do most prepared statement APIs in other languages:. According to MySQL documentationyou do not need to quote backtick identifiers using the following character set:. You can use characters beyond that set as table or column identifiers, including whitespace for example, but then you must quote backtick them.

And then there is " which is a special case. The query will select the string literal "column" where column foo is equal to string "bar". The query will select the column column where column foo is equal to column bar. There are good answers above regarding the SQL nature of your question, but this may also be relevant if you are new to PHP. Perhaps it is important to mention that PHP handles single and double quoted strings differently Single-quoted strings are 'literals' and are pretty much WYSIWYG strings.

Double-quoted strings are interpreted by PHP for possible variable-substitution backticks in PHP are not exactly strings; they execute a command in the shell and return the result.

Backticks are generally used to indicate an identifier and as well be safe from accidentally using the Reserved Keywords. Here the back make make money money paid ptcsite.biz will help the server to understand that the database is infact the name of the database, not the database identifier.

Same can be done for the table names and field names. This is very good habit if you wrap your database identifier with a backticks. Check this answer to understand more about backticks. Here I have deliberately forgot to wrap the title1 with a quotes. Now the server will take the title1 as a column name i. So, to indicate its a value you have to bucharest stock exchange trading calendar either double or single quotes.

PHP-MySQL free course, online tutorials PHP MySQL code

Now, in combination with PHP, double quotes and best coal stocks to buy 2016 quotes make your query writing time so easier. Lets see a modified version of the query in your question.

In combination of PHP and MySQL, double quotes and single quotes make your query-writing time so much easier. So if your string contains foreign currency trading foreign exchange online forex11 quotes, then you could use double make money wordpress multisite to quote the string, or if it contains double quotes, then you could use single quotes to quote the string.

But if your string contains both single quotes and double quotes, you need to escape the one that used to quote the string. Mostly, we use single quotes for an SQL string value, so we need to use double quotes for a PHP string.

Although a prepared statement is better. These identifier use only for string not aggregate function like now or sum ,max etc. Php mysql select single value from table you are denoting a column name with a table name then use back ticks like this:.

Backticks are generally used to indicate an identifier and as well be safe from accidentally using the reserved keywords. In combination of PHP and MySQL, double quotes and single quotes make your query writing time so much easier. Backticks are to be used for table and column identifiers, but are only necessary when the identifier is a MySQL reserved keyword, or when the identifier contains whitespace characters or characters beyond a limited set see below It is often recommended to avoid using reserved keywords as column or table identifiers when possible, avoiding the quoting issue.

There is a case though where an identifier can neither be a reserved keyword or contain whitespace or characters beyond limited set but necessarily require backticks around them. Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now fx brokers that accept paypal 10 reputation on this site the association php mysql select single value from table does not count.

Would you like to answer one of these unanswered questions instead? Stack Overflow Questions Developer Jobs Documentation beta Tags Users. Sign up or log in to customize your list.

Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us.

Log In Sign Up.

MySQL Cheat Sheet

Join the Stack Overflow Community. Stack Overflow is a community of 7.

php mysql select single value from table

Join them; it only takes a minute: When to use single quotes, double quotes, and backticks in MySQL Ask Question. What is the standard for this? What do you do? MYGz 6, 4 10 Nate 6, 18 70 SQL in general i. Back-ticks are never used in standard SQL. If you need to include a double quote in an identifier, type it twice as "odd""tablename".

Similarly, double single quotes in literals, like 'Conan O''Brien'. The most popular APIs available in PHP, PDO and MySQLi, expect unquoted placeholders, as do most prepared statement APIs in other languages: According to MySQL documentationyou do not need to quote backtick identifiers using the following character set: Michael Berkowski k 28 Leaving them unquoted results in them being interpreted as integer arithmetic.

It is true that different delimiters than - may be used, but quoting is necessary unless no delimiters are present dev. There are two types of quotes in MySQL: Salman A k 49 But, to define a value you have to use either single or double quotes.

Lets see another example. Therefore double quotes are flexible but can't be used as identifiers. Please never ever directly use unescaped user input in your query! I specified that if you are using direct user input then n then The string literals in MySQL and PHP are the same. Kumar Rakesh 2, 2 8 If table cols and values are variables then there are two ways: With double quotes "" the complete query: If you are denoting a column name with a table name then use back ticks like this: There has been many helpful answers here, generally culminating into two points.

select and echo a single field from mysql db using PHP - Stack Overflow

QUOTES ' are used around values. AND as MichaelBerkowski said Backticks are to be used for table and column identifiers, but are only necessary when the identifier is a MySQL reserved keyword, or when the identifier contains whitespace characters or characters beyond a limited set see below It is often recommended to avoid using reserved keywords as column or table identifiers when possible, avoiding the quoting issue.

EXAMPLE E10 is a valid identifier name but also a valid INTEGER literal. No ERROR on backticks. The Magician 1 9. Stack Overflow works best with JavaScript enabled. Note that this is a very MySQL specific question. MathOverflow Mathematics Cross Validated stats Theoretical Computer Science Physics Chemistry Biology Computer Science Philosophy more 3.

Meta Stack Exchange Stack Apps Area 51 Stack Overflow Talent.

inserted by FC2 system