Problem with HTML Manipulation with PHP

Asked

Viewed 44 times

2

People in the part of echo <p class="play">$row[/"email/"] </p> gives 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 to it, without it I cannot 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();
            ?>

  • almost you do not miss, put the counter bars on $row[\"email\"] instead of putting in the class="play". a solution echo "<p class=\"play\"> $row["email"]</p>";

1 answer

3


You need to use the slider to escape the double quotes in the class:

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

Or you can just swap the double quotes for single quotes

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

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

  • Dude Thanks for the tips, I don’t know Oce knows but would save too much+. Calling this echo $Row 'email' is taking everyone from the database. How can I pick up only one of the lines randomly? <3 thanks too much

  • The easiest and fastest way is to exchange your sql for: "SELECT email FROM news ORDER BY RAND() LIMIT 1".

  • If the answer worked, mark it as right to help other users with the same question in the future :)

  • 1

    Yes, indeed this consultation SELECT email FROM news ORDER BY RAND() LIMIT 1 will return random data from the table, but if in a PHP script you need, for example, to run the same query in a very short time interval it will always return the same data! The problem is that most servers have an internal clock type that is used to choose random table input. If you open a page that has this query and then open the same page with another browser, it will work without problems, after all you will be opening another browser session.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.