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

<?php

 $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";
 $result=mysql_query($sql);
 ?>


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


<?php


// Start looping rows in mysql database.
 while($rows=mysql_fetch_array($result)){
 ?>

 <tr>
 <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>
 </tr>

<?php
// close while loop 
 }

</table>

?>




<?php
// close MySQL connection 
 mysql_close();
 ?>

Also move line 53 out side of your php tag to line 56.

Or change it to

echo "</table>";
Member Avatar

diafol

OR

<?php
// close while loop 
 }
?>
</table>

I wish these tutorials were updated

Nothing to do with that. :)

Hi, I don't see any short tags in the code I posted except for the closing ones and those in the echo rows so I don't understand what to correct. Also is very difficult for me to figure out the line numbers because I don't have numbers in my notepad editor. I tried to move things around like you said but no success.Could you please if is not too much trouble amend the whole code for me so I don't have to guess what to move and where.Thank you very much.

It's about the tag </table> being in between the PHP code tags (<?php and ?>). Either move the table tag after ?> or change it to use an echo as stated above.

use 
<?php 
//close while loop
    }
    echo "</table>";
?>
or
<?php 
//close while loop
    }
?>
</table>

It's about the tag </table> being in between the PHP code tags (<?php and ?>).

@pritaeas, ardav & Squidge: totally right, for some reason I confused the previouses lines of code, my apologies.

Hi thank you everybody.Unfortunately none of your suggestions are working now I get the table displayed but not the content.I think the problem is further up in the code maybe the echo rows? I cannot figure it out.

Put a var_dump($rows) in your while loop , see what it says

Member Avatar

diafol

none of your suggestions are working

Yes they are. As you note, you have another problem upstream. The fact that you can't display records is a separate issue and one you did not mention initially.

Has your first Parse error gone?

Did you change, as has been advised, the short tags?

<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>

To be:

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

Does the user you are using in the DB connection have access to the database? Is the table created?

Also change your lines:

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

to:

mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");

Hi guys, finally it's working after making all the changes to the code like you suggested now it displays the table with all the data from the db as I wanted.Thanks a lot. Shortly I'll be bothering you guys again when I get to the next phase of m y project.Thank you