This error comes for mismatch use of parenthesis but i didn't succeed in tracking it

Running this code, i am getting unexpected $end error
kindly track the error and let me know with the required change code.....

<?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);

// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){
?>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<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>
</table>

<?
// close while loop
}

// close connection
mysql_close();
?>

I don't see anything specifically wrong with your php. But I did change short open tags and put a space before "or die".

The one thing you do want to change though is the placement of your <table></table> tags. They should be outside your while loop as you don't want them for every row (unless you really did want every row in its own table).

<?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%"><?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>
	</tr>
<?php
// close while loop
}
?>
</table>
<?php
// close connection
mysql_close();
?>
commented: Very well, i am impressed as he replied within few hours of my post!! Post error was easy but he gives me the solution so i must salute him +0

WELL! WITHOUT CHANGING YOUR CODE as LethargicCoder, did some little changes(he will be displaying rows inside table instead of tables as ur code)
I JUST CHECKED THE LINE 29,AND PUT SOMETHING LIKE THIS <?PHP.

<?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);

// Start looping rows in mysql database.
while($rows=mysql_fetch_array($result)){
?>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<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>
</table>

<?php
// close while loop
}

// close connection
mysql_close();
?>

I JUST CHECKED THE LINE 29,AND PUT SOMETHING LIKE THIS <?PHP.

Well you forgot to write PHP start tag.
Next time you post question, put the error message as well

Member Avatar for rajarajan2017
<?php
// close while loop
}

// close connection
mysql_close();
?>

There is no issue on braces, only one is opened and closed. as the above reply says thats the problem. I think so.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.