Confused - Generate pages into a template?

I have this script…

<?php

//CONNECT TO THE DATABASE-------------------------------------------------------------------------

$con = mysql_connect("xxxxx", "xxxxxxx", "xxxxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("xxxxxxx", $con);



//GET DATA FROM DATABASE --------------------------------------------------------------------------

$title_query = mysql_real_escape_string($_GET['product_code']);
$prods = mysql_query("
   SELECT product_code, id, name, description, image
   FROM products
   WHERE product_code='$title_query'
");

    
//DISPLAY PRODUCT INFORMATION  --------------------------------------------------------------------   
{
  	while($row = mysql_fetch_array($prods)) 
  {
    echo $row['product_code'] . "<br />";
    echo $row['id'] . "<br />";
    echo $row['name'] . "<br />";
    echo $row['description'] . "<br />";
    echo "<img src='http://www.xxxxxxxxxx.co.uk/shop_images/product_thumbs/".$row['image'] . "' /><br />";
  }
} 



?>

So now this script will generate product information when I call the URL containing the product_code. What I was wondering is…

Would it be best to mix the HTML into this script or should I make a seperate template somehow to do this?

As you can probably tell I am brand new to PHP in the last few days. Can anyone give me some advice please?

Thanks for that :slight_smile:

I am a complete noob though. With the script above how would I use it with a HTML template? let’s say I make a file called product_template.php containing the main html for the product pages; which bits of my script do I need to mix with the HTML ?

I can’t get my head round this bit?

It is certainly a good idea to separate out the display logic and the business logic. This lets you edit the html without worrying about messing up the inner workings of the script and means your code is reusable (you can call the same code with different templates)