$sql = ‘INSERT INTO joke SET
joketext="’ . $joketext . ‘",
jokedate=CURDATE()’;
from page 140 of Yank’s DRIVEN book…wow!..what is with the concatenation and double and single quotes there???
Now, i do understand that $joketext is a variable and that it has to be surrounded by double quotes to be interpolated. I understand that.
but the concatenation operator tacks strings together. I understand that $joketext represents a string. Fine. But if there are two concat operators, as we have above, then there must be THREE strings up there. Let’s say that $joketext is one. Then what are the other two strings being concatenated with $joketext ??
Is " ’ one string and ’ " another string? I do not think so…
WELL, if I had tested it, and if it had worked, I would have never asked that question. And if it had not worked, then I would not have asked that question either. So, you see, the answer to your question is inherent in my question.
I see. The first block of text makes it clear what the three strings are. I completely missed the outer single quotes. Never saw them. But in your second block of text, why put single strings around $joketext?
So I was wrong saying that you must use single quotes around strings, it seems double quotes are also allowed in MYSQL.
The advantage of using double quotes to surround the entire query, and single quotes to surround the string values in the query, is that you don’t have to concatenate variables as you can see in my example.
seems to me that THIS is the way the code should have been written:
$sql = ‘INSERT INTO joke SET
joketext=’ . " $joketext" .
‘, jokedate=CURDATE()’;
that way $joketext gets interpolated by the double quotes when this above is executed. And then the concat operator glues together the entire string, which consists of this:
‘single quote string’ . “double quote string” . ‘single quote string’;
remember, it is the php interpreter that is intepolating the $joketext variable, not mysql, and that interpolation must happen BEFORE the entire string is concatenated together.
but at this point in the book, he had not introduced the real escape function, so let’s just pretend that there are no characters in the string that need escaping. And let us ignore what is the more desirable INSERT syntax.