3

I am trying to query and select data from an sql table and then display it in html. This is my code.

$result = mysqli_query($dbhandle, $query)
    or die(mysql_error().'<br>SQL:'.$query);
echo '<table style ="border:1px solid black;">';

while($row = mysqli_fetch_array($result)) {
    echo '<tr>';
    echo '<td style="border:1px solid black;">{$row['first_name']}</td>';
    echo '<td style="border:1px solid black;">{$row['last_name']}</td>';
    echo '<td style="border:1px solid black;">{$row['grade']}</td>';
    echo '<td style="border:1px solid black;">{$row['gpaP']}</td>';
    echo "<td style="border:1px solid black;">{$row['AGP']}</td>";
    echo "<td style="border:1px solid black;">{$row['awardP']}</td>";
    echo "<td style="border:1px solid black;">{$row['awardTP']}</td>";
    echo "<td style="border:1px solid black;">{$row['rigorP']}</td>";
    echo "<td style="border:1px solid black;">{$row['APP']}</td>";
    echo "<td style="border:1px solid black;">{$row['positionP']}</td>";
    echo "<td style="border:1px solid black;">{$row['hoursP']}</td>";
    echo "<td style="border:1px solid black;">{$row['selectionP']}</td>";
    echo "<td style="border:1px solid black;">{$row['activityTypeP']}</td>";
    echo "<td style="border:1px solid black;">{$row['activityYearsP']}</td>";
    echo "<td style="border:1px solid black;">{$row['date']}</td>";
    echo "<td style="border:1px solid black;">{$row['IP']}</td>";
    echo '</tr>';
}
echo '</table>';

It gives me an error saying there is an unexpected string at the last_name cell. If I remove the style parameters from the cells it works. Somebody point out my error please?

3 Answers 3

3

Your single and double quptes are getting mixed up between the PHP and HTML. The easiest way to fix it is to use the heredoc syntax:

echo <<<EOT
<tr>
<td style="border:1px solid black;">{$row['first_name']}</td>
<td style="border:1px solid black;">{$row['last_name']}</td>
<td style="border:1px solid black;">{$row['grade']}</td>
<td style="border:1px solid black;">{$row['gpaP']}</td>
<td style="border:1px solid black;">{$row['AGP']}</td>
<td style="border:1px solid black;">{$row['awardP']}</td>
<td style="border:1px solid black;">{$row['awardTP']}</td>
<td style="border:1px solid black;">{$row['rigorP']}</td>
<td style="border:1px solid black;">{$row['APP']}</td>
<td style="border:1px solid black;">{$row['positionP']}</td>
<td style="border:1px solid black;">{$row['hoursP']}</td>
<td style="border:1px solid black;">{$row['selectionP']}</td>
<td style="border:1px solid black;">{$row['activityTypeP']}</td>
<td style="border:1px solid black;">{$row['activityYearsP']}</td>
<td style="border:1px solid black;">{$row['date']}</td>
<td style="border:1px solid black;">{$row['IP']}</td>
</tr>
EOT;

This syntax makes it much easier since you can use both single and double quotes without escaping them and variables will be evaluated. Also, in your original code, the variables aren't going to be evaluated since they're in single quotes.

Sign up to request clarification or add additional context in comments.

Comments

2

Your code mixes two different styles of quotes, both of which are valid in PHP and both of which require some additional escaping.

You should pick a style (either wrap in single quotes or wrap in double quotes) and then make sure you escape quotes of the same type within the string.

Comments

1

You are mixing single quotes ' with double quotes "". For example, if you start the line with " and then use it again before the end of the line, you need to escape it with \". My suggestion is for you to stick with one style, and use it for all the lines. For example, usign "":

  echo "<td style=\"border:1px solid black;\">{$row['first_name']}</td>";
  echo "<td style=\"border:1px solid black;\">{$row['IP']}</td>";

4 Comments

@lonewaft: You were probably starting the string with "" instead of ' in that case.
@lonewaft The other lines are also incorrect. You have a double quote to open the argument to echo and you are using double-quotes to set the styles. You should use single quotes for the styles in this case.
Uhh, there is a huge problem with that.. If i escape the quotes, instead of showing the data inside the variable, it shows the variable name itself..
I ended up going with @PeterGluck's solution, and its working perfectly now without escape quotes, but thanks anyways!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.