Database connect and display

I’m trying to connect to a MySQL database and display the contents on a page using functions. I, however, have been stuck for a while on this issue.

The following code is the connection function.

 function dataConnect() {
	$server = '';
	$myDB = '';
	$username = '';
	$password = '';
	$dsn = 'mysql:host=$server;dbname=$myDB';

	try {
		$db = new PDO($dsn,$username,$password);
		
		if ($db) :
		echo "Connected to the " . $myDB . "database successfully!";
		return $db;
		endif;

	} catch(PDOException $e) {
		echo $e->getMessage;
	}

 }

This script is the recipe display script.

function allRecipesDisplay() {
	/** This function is used to call forth all recipes from the database and display them
	  * as a catalog.
	**/

	$db = dataConnect();

	$count = "SELECT count(recipeid) FROM recipes";
	$recipeCount= $db->query($count);
	$row   = $recipeCount->fetchAll(PDO::FETCH_ASSOC);

	$recipeQuery = $db->query('SELECT recipeid, catid, title, poster, spicer, shortdesc FROM recipes');
	$recipes = $recipeQuery->fetchAll(PDO::FETCH_ASSOC);


			echo "<h1>Recipe Catalog</h1>\n";
			
		$recipeid = $recipes['recipeid'];
		$catid    = $recipes['catid'];
		$title    = $recipes['title'];
		$poster   = $recipes['poster'];
		$spicer   = $recipes['spicer'];
		$shortdesc= $recipes['shortdesc'];
		
		if($spicer == null) {
				echo "<table width=\"100%\" cellpadding=\".5px\" \n";
				echo "cellspacing=\"1px\" border=\"0\" align=\"center\">\n";
				echo "<tr><td rowspan=\"3\"><img src=\"showimage.php?id=$recipeid\" \n";
				echo "width=\"80\" height=\"60\"></td>\n";
				echo "<td><a href=\"index.php?card=showrecipe&id=$recipeid\">\n";
				echo "$title</a><catname>$category</catname></td></tr>\n";
				echo "<tr><td><font size=\"1\" color=\"#ff9966\">posted by: \n";
				echo " <em>Chef $poster</em></font></td></tr>\n";
				echo "<tr><td><p>$shortdesc</p></td></tr>\n";
				echo "<tr><td colspan=\"2\"><hr></td></tr></table>\n";
		} else {
				echo "<table width=\"100%\" cellpadding=\".5px\" \n";
				echo "cellspacing=\"1px\" border=\"0\" align=\"center\">\n";
				echo "<tr><td rowspan=\"3\"><img src=\"showimage.php?id=$recipeid\" \n";
				echo "width=\"80\" height=\"60\"></td>\n";
				echo "<td><a href=\"index.php?card=showrecipe&id=$recipeid\">\n";
				echo "$title</a><catname>$category</catname></td></tr>\n";
				echo "<tr><td><font size=\"1\" color=\"#ff9966\">posted by: \n";
				echo "<em>Chef $poster</em> and spiced by: <em>Chef $spicer</em>\n";
				echo "</td></tr>\n";
				echo "<tr><td><p>$shortdesc</p></td></tr>\n";
				echo "<tr><td colspan=\"2\"><hr></td></tr></table>\n";
		}
	}
}

Also, the dataConnect() function is in it’s own PHP file and the display function is in a file with two other functions. In total I’ll have at least three display functions. One function will be used to display all recipes, another will be filtered them based on who is logged in and the final will filter out all recipes of the who is logged in. I hope I’ve been informative of how I would like this to work.