-1

Issue

I'm currently attempting to create a "cell" for each mysql row similar to how twitter has it, but for some reason i'm logging

PHP Parse error:  parse error, expecting `','' or `';''

Code

I'm fairly new to PHP I am unsure of why my code is creating this error.

<?php

while ($row = mysql_fetch_array($rs)) {
    $unformatted = $row['mcap_HKD'];
    $output = number_format($unformatted);
    $time = time_elapsed_string($row['time']);
    echo
        "<div class="content">
            <div class="headers">
                <div class="info">
                    <strong class="scode">".$row['scode']."</strong><br>
                    <span class="sname">".$row['sname']."</span><br>
                    <span class="industry">".$row['main_industry']."</span>
                </div>
                <small class="time">."$time."</small>
            </div>
            <div class="news">
                <a href="">".$row['title']."</a>
            </div>
        </div>";
}

I know that through testing that I am able to reach the server and that it will output the data just fine on its own, and even when i output single variables onto one div, but for some reason this is not working when i put extra divs like i do above. I have attempted to echo each line by itself but it still returns the same error.

Alexandre
  • 474
  • 2
  • 14
kimpster
  • 77
  • 2
  • 12
  • 1
    you must escape `"` characters in the `echo` statement as such: `\"` – Alexandre Dec 12 '16 at 08:25
  • @Alexandre correctly stated you have to take care to escape contained double quotes in a string if you enclose it with double quotes. How else should php know where the string actually stops? Start like that: `"
    ...`
    – arkascha Dec 12 '16 at 08:27
  • Possible duplicate of [PHP Parse/Syntax Errors; and How to solve them?](http://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – u_mulder Dec 12 '16 at 08:42
  • This has nothing to do with MySQL. You have syntax error in your PHP code instead. Make sure you have escaped your quotes properly. You need to escape special characters like double quotes by doing `\"` when you're using it inside quotes. – kabirbaidhya Dec 12 '16 at 10:31

1 Answers1

2

Issue

PHP use " as a string delimiter (it has several string delimiters). When, in you echo statement, you are not escaping the ", it's as if you stop the string. So PHP is waiting for a string concatenation . or , or the ; end of statement character.

Resolution

You must escape " characters in you echo statement as such: \" ; it should look like:

Escape string delimiters

echo
    "<div class=\"content\">
        <div class=\"headers\">
            <div class=\"info\">
                <strong class=\"scode\">".$row['scode']."</strong><br>
                <span class=\"sname\">".$row['sname']."</span><br>
                <span class=\"industry\">".$row['main_industry']."</span>
            </div>
            <small class=\"time\">."$time."</small>
        </div>
        <div class=\"news\">
            <a href=\"\">".$row['title']."</a>
        </div>
    </div>";

Mix HTML and PHP

or do something like:

<?php while ($row = mysql_fetch_array($rs)): ?>
    <?php
        $unformatted = $row['mcap_HKD'];
        $output = number_format($unformatted);
        $time = time_elapsed_string($row['time']);
    ?>
    <div class="content">
        <div class="headers">
            <div class="info">
                <strong class="scode"><?= $row['scode']; ?></strong><br>
                <span class="sname"><?= $row['sname']; ?></span><br>
                <span class="industry"><?= $row['main_industry']; ?></span>
            </div>
            <small class="time"><?= $time; ?></small>
        </div>
        <div class="news">
            <a href=""><?= $row['title']; ?></a>
        </div>
    </div>
<?php endwhile; ?>

This has the benefit to not use PHP to print HTML tags, and with the <?= PHP opening tags, it's more readable!

I hope this helps you! :)

Alexandre
  • 474
  • 2
  • 14