Page 1 of 1

PHP Parse error: parse error, unexpected T_VARIABLE

Posted: Mon Sep 25, 2006 10:42 pm
by Tyronem
Hi all, just starting out on my road to php - got php, mysql and IIS installed on my cough cough windows pc, I can connect to the database via php fine, and php is working on the system.

My problem is I am trying to create a product menu pulled from a database - the code is as follows below

- the error which is in the title referrs to the variable $count

It's probably something blaringly obvious I have missed :)

Code: Select all

<?php 
	
	// include the header for the homepage located in the file header_home_page.php
	include "extras/header_home_page.php" 

?> 

<body>
    

    <div>
    
        <div id="top_strip">
    
                <div id="logo"> 
            
                </div>
    
 </div>
        
     <div id="menu">
        
          <h2>Main Menu</h2>
					
                  <?php
					
				//include "database/database_connection_string.php"
								
				//Declare Count Variable
				$count = 0; //This is used to avoid that "|" is printed before the first menu item
				
                                 // Get the data into the staging part of the system (not yet available to php
				// or MySQL
							
                               $query = mysql_query("SELECT * FROM product ORDER BY Product_ID ASC")
                                or die(mysql_error());
							

                               while ($menu_row = mysql_fetch_object($query)) 
  				   { 
    					if ($count!=0){echo " | ";} // display the | after the menu name
    //output the menu name (.$menu_row->Menu.Name.) and on click  redirect to product_display.php page and 
									
   //pass the variable product_ID to the page (?Product_ID=$menu_row->Product_ID\") then goto a new line   (<br>)
									

                             echo "<a href=\"product_display.php?Product_ID=$menu_row->Product_ID\">.$menu_row->Menu_Name.</a><br>";
    				
                                //count = count + 1
				$count++;
  				
}

?>
       
// dont need rest of page code

also

Posted: Mon Sep 25, 2006 10:44 pm
by Tyronem
forgot to mention header include works fine

Re: PHP Parse error: parse error, unexpected T_VARIABLE

Posted: Mon Sep 25, 2006 10:57 pm
by scorphus
Hi.

I think there is a typo in the echo line.

You could try this:

Code: Select all

<?php 
//...
echo '<a href="product_display.php?Product_ID='.$menu_row->Product_ID.'">'.$menu_row->Menu_Name.'</a><br>';
//
?>
or yet maybe:

Code: Select all

<?php 
//...
echo "<a href=\"product_display.php?Product_ID={$menu_row->Product_ID}\">{$menu_row->Menu_Name}</a><br>";
//
?>
And tell us what happens.

-- scorphus

lol solved it :)

Posted: Mon Sep 25, 2006 10:58 pm
by Tyronem
It was Something to do with including the database connection in the php tags if i verbosely do it in a seperate menu include and include the menu.php file it works fine - if anyone knows why feel free to post Its all learning :)

Cheers

thanks schorphus

Posted: Mon Sep 25, 2006 10:59 pm
by Tyronem
You were also right about the quotes the correct working line was:

echo "<a href=\"product_display.php?Product_ID=$menu_row->Product_ID\">$menu_row->Menu_Description</a><br>";

Cheers!!

code

Posted: Mon Sep 25, 2006 11:02 pm
by Tyronem
total code looks like this now:

<?php

include "classes/menu.php"

?>

Menu Page:

Code: Select all

<?php

/* connect to the localhost database - It doesnt look like much but this will connect you trust me <br>
use the username webuser and the password secret and make the connection persistant. The @ symbol
forces the function to use the custom error message below itself */

@mysql_pconnect ("localhost","root","#####")  // using mysql_pconnect means that php will check to see 
//													if the database connection exists. If it doesnt then 
//													it will create one but if it does then it won't worry 
//													about creating another one and will just use the open
//													connection.
or die("Hard luck no such database connection"); // Error message to be displayed if no connection exists
echo 'Genius! Connected to the database'; // this displays regardless but if you get the error message
//												something is wrong

// Connect to the correct database
@mysql_select_db("tyrone")
   or die("Could not select database!");

	
							//Declare Count Variable
							$count = 0; //This is used to avoid that "|" is printed before the first menu item
							// Get the data into the staging part of the system (not yet available to php
							// or MySQL
							$query = mysql_query("SELECT * FROM products ORDER BY Product_ID ASC") or die(mysql_error());
							while ($menu_row = mysql_fetch_object($query)) 
  								{ 
    								if ($count!=0){echo " | ";} // display the | after the menu name
    								//output the menu name (.$menu_row->Menu.Name.) and on click redirect to product_display.php page and 
									//pass the variable product_ID to the page (?Product_ID=$menu_row->Product_ID\") then goto a new line (<br>)
									echo "<a href=\"product_display.php?Product_ID=$menu_row->Product_ID\">$menu_row->Menu_Description</a><br>";
    								//count = count + 1
									$count++;
  								}
							
							
?>

Posted: Mon Sep 25, 2006 11:16 pm
by shneoh
You have missed a ";"...

include "extras/header_home_page.php";

Posted: Mon Sep 25, 2006 11:26 pm
by scorphus
Actually, if your PHP statement block is just one line length there is no need for a ; ;)

i.e.:

Code: Select all

<?php

// One line statement:

echo "Tadam!"

// No ;

?>
-- scorphus

Re missed the ";"

Posted: Tue Sep 26, 2006 7:22 am
by Tyronem
shneoh wrote:You have missed a ";"...


include "extras/header_home_page.php";
quite right i had too. lol, ahh now my page just has a menu include to run the menu and in the menu include it has the database include (with the ";") and it works as sweet as candy, thanks guys

Posted: Tue Sep 26, 2006 9:44 pm
by shneoh
You are welcome. :D