1

I tried the same script on my Linux environment (I am not running on Windows), and it worked flawlessly. The point of the script (although irrelevant) is to print out the contents of a table.

The script is as follows:

table_contents.php

<?
$user="root";
$password="";
$database="newtest";
$con = mysqli_connect(localhost,$user,$password,$database);
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$query="SELECT * FROM contacts";
$result = mysqli_query($con,$query);

$num = mysqli_num_rows($result); // COUNT for FOR LOOP

echo '<table border=\'1\'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>'; //This is where it starts printing out everything.

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['first'] . "</td>";
  echo "<td>" . $row['last'] . "</td>";
  echo "</tr>";
  }

mysqli_close($con);
?>

And the table_contents.php displays the following line on my browser:

Firstname Lastname '; while($row = mysqli_fetch_array($result)) { echo ""; echo "" . $row['first'] . ""; echo "" . $row['last'] . ""; echo ""; } mysqli_close($con); ?>

Indicating that the commented line in the script above is the point after which is throws everything out to be displayed.

Why is this being caused and how can I fix this?

karthikr
  • 97,368
  • 26
  • 197
  • 188
Louis93
  • 3,843
  • 8
  • 48
  • 94

2 Answers2

8

Your server is not configured to accept short tags (<?). Therefore, the browser is seeing everything from that <? to the first > as a very long, very invalid HTML tag. Anything after that is seen as text.

You should use the full <?php to open a PHP code block.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • 2
    Short tags can be enabled in `php.ini` with `short_open_tag=On` or in a *.htaccess* file with `php_value short_open_tag 1` if PHP runs as a Apache module. – ComFreek Jun 25 '13 at 17:51
  • 2
    @CornFreek, but you really shouldn't. – Jason McCreary Jun 25 '13 at 17:52
  • @JasonMcCreary Yes, I definitely recommend using the full tag. Here is a more in-depth discussion: http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use – ComFreek Jun 25 '13 at 17:55
  • I use short open tags and don't think there is anything wrong with it. Great when writing view code. Rather use short tags than Smarty any day. Although, "Since PHP 5.4.0, = is always available." which gets you most of the bang. – ficuscr Jun 25 '13 at 18:00
1

Try using <?php instead of using <? or, if you do not want to alter your scripts: change the option short_open_tag in php.ini (set to on)

ggouweloos
  • 26
  • 3