Quantcast
Viewing latest article 20
Browse Latest Browse All 51

Answer by andrewsi for populate html table with php from mySQL based on user input

$sql = "SELECT test1,'$montreal' AS testX FROM test WHERE test1='$zurich'";

Mark's answer is correct, but not for the reason he gives.

He says that the above will treat '$montreal' as a string literal. Normally that is correct, but because it's embedded in a double quoted string, it will actually be interpolated correctly.

What's actually happening is that you're generating a string along the lines of:

SELECT test1,'fieldname' AS testX FROM test WHERE test1='$zurich'

The 'fieldname' is incorrect - it's a column name. Single quotes in an SQL query are for values, so MySQL isn't interpreting this as you expect. If you want to use quotes for a column (or table) name, you need to use backticks:

$sql = "SELECT test1,`$montreal` AS testX FROM test WHERE test1='$zurich'";

Will work. Though as you noticed, you can get the same effect by omitting any quotes.


Viewing latest article 20
Browse Latest Browse All 51

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>