Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialChris Shearon
Courses Plus Student 17,330 PointsSomething I'd like to point out when concatenating a string to the query.
At the 4:18 mark he adds the variable from $_GET to the where clause in the query. This is fine for integers but for strings it must be enclosed in quotation marks. For example: $results = $dbh->query ( 'Select * from visiter_comments where first_name = "' . $firstNameid . '"' );
2 Answers
Roberto Rodríguez
48 PointsHi!
You can use double quotes when you send the SQL query as an argument of the query method, remember that single quotes will display things almost completely as is and you need to use the concatenation operator to embed a variable in the string, if you do not want to use the concatenation operator you can use double quotes where variables in the strings will be parsed within it.
Your code can be written in this other way:
<?php
$results = $dbh->query ("Select * from visiter_comments where first_name = $firstNameid");
?>
Using this form is recommended to use curly braces to isolate the name of the variable you want evaluate for improved code reading:
<?php
$results = $dbh->query ("Select * from visiter_comments where first_name = {$firstNameid}");
?>
You could also make use of the curly braces to isolate the name of your variable where may be continuous to another letter or word, let us look at an example:
<?php
$fruit = 'apple';
// Outputs: I love apples!
echo "I love {$apple}s!";
?>
You can read more about string parsing in PHP: Single quoted, PHP: Double quoted and PHP: Variable parsing.
I hope my explanation has helped you!
Mark Buskbjerg
20,986 PointsThanks Chris Shearon. This is vital information not covered in the video.
You just made my day so much easier :)