-1

I am currently writing a dummy comment/answer system using PHP/SQL/AJAX and have come an issue on my output divs due to my coding style syntax.

To simplify the code I have just included the while loop: //prep query $sqlQuery = "SELECT * FROM Questions ORDER BY QuestionID DESC";

    //exec query
    $result = mysql_query($sqlQuery) or die(mysql_error());

    //loop for multipule results
        while ($row = mysql_fetch_array($result))
    {
        echo '<div class="test" id="test_$questionID">'; //<--doesn't work
        //code here
        echo '</div>';

        echo "<div class='test' id='test_$questionID'>"; //<--works
        //code here
        echo '</div>';
    }

Output of failed code line:

<div class="test" id="test_$questionID">

Output of successful code line:

<div class='test' id='test_21'>

The code above works when I start an echo using " instead of ' my PHP style is to start each outer with ' and innners with ".

Should I change my div echos to start with " or is there another way?

Edit: Thanks for all the quick replies all!

I was taught to start using singles instead of doubles without the actual reason behind it.

I didn't know the conflict was caused because of the text being interpreted incorrectly and the environment wasn't throwing any errors to guide. Escaping them as @PressingOnAlways or changing them as @santhy recommend.

It's not that I was asking the difference between singles and doubles since I didn't know that was the area of error. I can see now that it was, but not sure I would have know to look for that stack article first sorry.

axiac
  • 68,258
  • 9
  • 99
  • 134
Ben
  • 295
  • 5
  • 19
  • in PHP the quotes matter - stuff between single quotes is not parsed for any values and is taken to be as-is. I.e. `$var = 'some $text';` is taken to literally be `some $text`, where as `$var = "some $text";` is parsed using whatever value is defined for `$text` variable at the time of parsing. – Jhecht Aug 04 '17 at 03:40

4 Answers4

2

You need to use double quotes, but escape the inside double quotes with backslash. For example:

 echo "<div class=\"test\" id=\"test_$questionID\">";
PressingOnAlways
  • 11,948
  • 6
  • 32
  • 59
1

Try this:

Replace echo '<div class="test" id="test_$questionID">'; //<--doesn't work line:

 <?php
 //exec query
    $result = mysql_query($sqlQuery) or die(mysql_error());

    //loop for multipule results
    while ($row = mysql_fetch_array($result))
    {
        echo '<div class="test" id="test_'. $questionID .'">'; //<--now work
        //code here
        echo '</div>';

        echo '<div class="test" id="test_'. $questionID .'">'; //<--now work
        //code here
        echo '</div>';
    }
Chandra Kumar
  • 4,127
  • 1
  • 17
  • 25
1

We can use different ways, see some of them

while ($row = mysql_fetch_array($result))
{
    echo '<div class="test" id="test_'.$questionID.'">';
    //code here
    echo '</div>';

    echo "<div class='test' id='test_{$questionID}'>";
    //code here
    echo '</div>';

    echo "<div class=\"test\" id=\"test_{$questionID}\">";
    //code here
    echo '</div>';

    ?>
    <div class="test" id="test_<?=$questionID;?>"> <?php
    //code here
    ?>
    </div> <?php
}
Santhy K
  • 829
  • 1
  • 7
  • 12
0

PHP doesn't do variable interpolation in strings enclosed by single quotes. You must use double quotes. Also, I make it a practice when interpolating a variable that combines with nearby characters, I wrap the variable in brackets for clarity.

So

 echo '<div class="test" id="test_$questionID">';

becomes:

 echo "<div class=\"test\" id=\"test_{$questionID}\">";
RichGoldMD
  • 1,252
  • 1
  • 10
  • 18