views:

326

answers:

6

I'm trying to read a comment that is stored in a mysql table. For some reason I always get a parse error on the last line of the file even if the last line is blank. I'm not sure if it's relevant but the connect.php works for putting the comment into the database. I'm using wampserver to host it and coding it by hand.

I think that's it's something to do with the while loop, when I comment out the while(){ and the } near the end I just get a few missing variable errors as you would expect. I'm quite new to php coding so I'm pretty sure the problem will be something simple that I've either missed or not understood properly.

Anyway, here's my code:

<?php
include "connect.php";
?>

<?php
$sql = "SELECT * FROM main";
$result = mysql_query($sql) or die("Could not get posts from table");
while($rows=mysql_fetch_array($result)){
?>
     <table bgcolor="green" align="center">
     <tr>
     <td></td>
     </tr>
     <tr>
     <td><strong> <? echo $rows['name']; ?> </strong></td>
     </tr>
     <tr>
     <td> <? echo $rows['email']; ?> </td>
     </tr>
     <tr>
     <td> <? echo $rows['comment']; ?> </td>
     </tr>
     </table>
<?
}
?>

Thanks for the help. :)

+5  A: 

Looks like you've the PHP short tags disabled. So

replace

<?

with

<?php

To make your program portable avoid using short tags. If you still have to use the short tags you can enable then. See this post to know how to do it.

codaddict
Thanks for your help, I didn't understand the difference between the normal tags and short tags. Changing them to <?phpsolved the issue.
is0lated
Its just that *PHP* supports many types of tags like: <?php [code here] ?> (std tag), <? [code here] ?> (short tag), <% [code here] %>(ASP style tag) but your *server* can be configured to not support the short tags. So an application having short tags will work only on the server where they are enabled. So to make your application portable, its advised to stick to the std tags.
codaddict
+4  A: 

Do you have short tags enabled? The bottom of your code looks like this:

<? 
} 
?> 

If short tags aren't enabled, you must open a php tag with <?php

<?php
} 
?>

Not having the correct opening tag would cause an error on the last line because it's expecting a } there. See also: http://www.bin-co.com/php/articles/using_php_short_tags.php

Andy E
A: 

If you include your file in some other php file try removing the closing PHP tag at the very end of your file (and, of course, use full tag names):

   ....<td> <? echo $rows['comment']; ?> </td>
 </tr>
 </table>
<?php
}
Yaroslav
A: 
Joe
A: 

I have been thrown off a couple of time by characters that are not what they seem. When I have copy pasted the code from browsers, i have found that deleting and retyping the ' and the " clears up the errors.

kinjal
A: 

The short way is not always available. The short tags is available with short_tags function () (PHP 3 only), setting the parameter of the configuration file of PHP short_open_tag, or by compiling PHP with the - enable-short -tags option to configure. Although it's enabled by default in php.ini-dist, it discourages the use of short tags format.

Although this might not be enough, the manual reiterate and also expanding other situations where it is not appropriate:

"Note: Do not use short tags when developing applications or libraries that are meant for redistribution, or when it is developed for servers not under your control, because it may be that short tags is not supported in the server. To build portable, redistributable code, be sure not to use short tags. "

In fact, in PHP 6 or 7, these tags will be removed.

BTW, In the Zend Framework manual clearly says "Short tags are never allowed" :)

feragusper