Solved PHP old code not working

Hewlett-packard / Cq61 notebook
July 7, 2013 at 05:44:26
Specs: Windows 7, AMD sempron 2 gb
Hi I need some help please. I am trying to display mysql data on webpage.I got this example from
PHPEasyStep but it does not work. I think the code is a few years old and not compatible with
my version of Php which is 5.4.Actually I have this problem with most tutorials and it is a shame
because it causes a lot of time consuming, I wish these tutorials were updated, would be much easier.
Anyway can somebody tell me how to correct the code below so that it does work? Thanks a lot.
This is the error I get:

Parse error: syntax error, unexpected '<' in C:\inetpub\wwwroot\select.php on line 44


$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Retrieve data from database
$sql="SELECT * FROM $tbl_name";

<table width="400" border="1" cellspacing="0" cellpadding="3">


// Start looping rows in mysql database.

<td width="10%"><? echo $rows['id']; ?></td>
<td width="30%"><? echo $rows['name']; ?></td>
<td width="30%"><? echo $rows['lastname']; ?></td>
<td width="30%"><? echo $rows['email']; ?></td>

// close while loop



// close MySQL connection

See More: PHP old code not working

Report •

July 8, 2013 at 00:03:21
✔ Best Answer
The closing table tag is within PHP elements, but isn't being echoed out. Move the closing PHP element on line 41 above the closing table tag on line 39, but below the right-curly-bracket on line 37.

When you echo out each column, you open the PHP tags using short open tags (<?) instead of the full tag (<?php). Before PHP 5.4, you had to enable short open tags within your PHP.ini file. Since PHP 5.4, they're enabled by default. If you're still having trouble, ensure they're properly enabled, or just always use the full tag.

You may also consider using mysqli functions instead of mysql because the latter is deprecated in PHP 5.5, though they'll work fine in PHP 5.4.

I'm happy to help debug any of those tutorials you mentioned. It's good practice for me so we'd both benefit. =)

Apologies if I don't respond to your reply immediately. I don't check this site daily, but you're welcome to PM me as a reminder.

Report •

July 10, 2013 at 05:05:29
Hi I finally got it to work, the main thing was to use the full php tag instead of the short one near the echo rows and moving the end table tag out of closing php tag.Thanks for your help, I don't know how to set as best answer, I looked everywhere.

Report •

Related Solutions

Ask Question