Problem with HTML Handling with PHP

1

Fetch in the echo <p class="play">$row[/"email/"] </p> part of a syntax error

  

Parse error: syntax error, unexpected 'play' (T_STRING), expecting ','   or in C: \ xampp \ htdocs \ a \ index.php on line 20

Does not recognize the class I give it, without it I can not format the results.     

            // Create connection
            $conn = new mysqli($servername, $username, $password, $dbname);
            // Check connection
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            } 

            $sql = "SELECT email FROM news";
            $result = $conn->query($sql);

            if ($result->num_rows > 0) {
                // output data of each row
                while($row = $result->fetch_assoc()) {
                    echo  "<p class="play"> $row[\"email\"]</p>";
                }
            } else {
                echo "0 results";
            }
            $conn->close();
            ?>
    
asked by anonymous 17.10.2018 / 19:01

1 answer

2

It is necessary to use the slash to escape the double quotation marks in the class:

echo  "<p class=\"play\"> {$row["email"]}</p>";

Or you can simply swap double quotation marks with single quotation marks

    echo  "<p class='play'> {$row['email']}</p>";

When using an array inside a string, it is always good to encapsulate the variable with {}.

    
17.10.2018 / 19:04