View Full Version : Why the error?

04-29-2006, 07:46 PM
Hi gang
have a look at this code. I'm getting this error when I run this:

"Parse error: parse error, unexpected T_ECHO in product.php on line 40"

Line 40 is highlighte in red below.


// set server access variables
$host = "xx";
$user = "xx";
$pass = "xx";
$db = "xx";
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// create query
$query = "SELECT name FROM product";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

if ($result)
$row = mysql_fetch_array($result);
echo "<table cellpadding=1 border=1 width=100%>";
echo "<tr>";
echo "<td>$row['name']</td>";
echo "</tr>";
echo "</table>";

04-29-2006, 07:57 PM
This bit looks odd to me, even though I think it should be ok.

$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

Just to make sure, try:

$result = mysql_query($query) or die ("Error in query: ".$query."<br/>".mysql_error());

I don't think that should be a problem, but it's the only 'niggle' I can see in what looks like perfect code.
The only other thing you could change is the "if ($result)", because if the query failed the script would die anyway, so there's no need to check.

04-29-2006, 08:01 PM
Thanks CD...
Still get the error... it's strange that this "echo" line produces an error... it's like it doesn't like HTML reference there...

Any other ideas?

04-29-2006, 08:03 PM
The only error I can see is
echo "<td>$row['name']</td>"; which should be
echo "<td>$row[name]</td>"; or
echo "<td>{$row['name']}</td>";

04-29-2006, 08:05 PM
Often the error is before the line that is given, because it is a syntax error previously that throws the interpreter off, and makes lines coming next to be 'unexpected'.

Do you intend to output only the first row returned from your query?

04-29-2006, 08:17 PM
No... this will be the first of many... I'm basically building an HTML table with a bunch of records from the database like product name, description, price, shipping cost...

Thanks! the same error occurs...


04-29-2006, 09:00 PM
Why not post the rest of the script as well. There is obviously more as there are not 40 lines there.

04-29-2006, 09:30 PM
that's it so far!!

I've included the entire <php ?> script... I figured that if we figured out 1 row of data, I would be able to figure out adding additional rows of of different records


Muhammad Haris
04-29-2006, 09:42 PM
echo "<table cellpadding=1 border=1 width=100%>";

should probably be

echo '<table cellpadding="1" border="1" width="100%">';

04-29-2006, 09:59 PM
echo "<table cellpadding=1 border=1 width=100%>";

should probably be

echo '<table cellpadding="1" border="1" width="100%">';
It'll make no difference. You can drop the quotes for single word attributes, although I never do.

@losse: Upload the file in question.

04-30-2006, 12:14 AM
Here it is attached... it's TXT format...

04-30-2006, 02:53 AM
Seems the code you posted and the code you uploaded are different...
Missed a semi-colon:
$row = mysql_fetch_array($result);

04-30-2006, 04:34 AM
Holy crap... that was it all along...
Thansk alot for your keen eye...

It now works!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum