Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13

Thread: Why the error?

  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Why the error?

    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.

    Thanks

    Code:
    <?php 
    // 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>"; 
    }
    ?>

  • #2
    New Coder
    Join Date
    Apr 2006
    Location
    UK
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This bit looks odd to me, even though I think it should be ok.
    PHP Code:
    $result mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 
    Just to make sure, try:
    PHP Code:
    $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.

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The only error I can see is
    PHP Code:
    echo "<td>$row['name']</td>"
    which should be
    PHP Code:
    echo "<td>$row[name]</td>"
    or
    PHP Code:
    echo "<td>{$row['name']}</td>"

  • #5
    New Coder
    Join Date
    Apr 2006
    Location
    UK
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #6
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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...

    trib4lmaniac:
    Thanks! the same error occurs...

    Thanks

  • #7
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why not post the rest of the script as well. There is obviously more as there are not 40 lines there.

  • #8
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

    ...

  • #9
    New Coder
    Join Date
    Apr 2006
    Location
    Pakistan
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    echo "<table cellpadding=1 border=1 width=100%>"
    should probably be

    PHP Code:
        echo '<table cellpadding="1" border="1" width="100%">'

  • #10
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Muhammad Haris
    PHP Code:
    echo "<table cellpadding=1 border=1 width=100%>"
    should probably be

    PHP Code:
        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.

  • #11
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Here it is attached... it's TXT format...
    Attached Files Attached Files

  • #12
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Seems the code you posted and the code you uploaded are different...
    Missed a semi-colon:
    $row = mysql_fetch_array($result);

  • #13
    Regular Coder
    Join Date
    Mar 2005
    Posts
    240
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Holy crap... that was it all along...
    Thansk alot for your keen eye...

    It now works!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •