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 trial

PHP

William Parasnis
William Parasnis
840 Points

Parse error PHP

My question is that I get a syntax error with the following code:

$query = "SELECT stad, modell FROM Biluthyrare a, Bilar b WHERE a.uthyrNr = b.uthyrNr AND stad = '".$stad"'";

The error is "Parse error: syntax error, unexpected '""' (T_CONSTANT_ENCAPSED_STRING) in ......

Any ideas?

2 Answers

This looks like it comes from mixing quotes. Two ways to change your query so that it will no longer throw that error are below.

The following example works because when you use double-quotes ("), you can simply add your variables in the string and the query will be formed how you intended.

<?php

$query = "SELECT stad, modell FROM Biluthyrare a, Bilar b WHERE a.uthyrNr = b.uthyrNr AND stad = '{$stad}'";

This one where I have simply added an extra concatenation after the variable will also work but in my opinion is significantly less clear about what is happening because we're gluing multiple portions of the string together with multiple quotation marks sitting right next to each other.

<?php

$query = "SELECT stad, modell FROM Biluthyrare a, Bilar b WHERE a.uthyrNr = b.uthyrNr AND stad = '" . $stad . "'";