I'm trying to echo out dynamic php titles depends on page id for seo purposes.
First of all, all of my pages includes header.php
And this is how my header.php
begins like:
include("database.php");
//connect tables in order to echo out titles
//1- connect to categories table if isset get category_id
if (isset($_GET["category_id"])) {
$query = $handler->query("SELECT * FROM categories WHERE category_id = ".$_GET['category_id']." ");
while($r = $query->fetch()) {
$title = $r["title"];
}
}
//2- connect to articles table if isset get article_id
if (isset($_GET["article_id"])) {
$query = $handler->query("SELECT * FROM articles WHERE article_id = ".$_GET['article_id']." ");
while($r = $query->fetch()) {
$title = $r["title"];
}
}
//3- connect to users table if isset get user_id
if (isset($_GET["user_id"])) {
$query = $handler->query("SELECT * FROM users WHERE user_id = ".$_GET['user_id']." ");
while($r = $query->fetch()) {
$title = $r["username"];
}
}
And after I feel free to echo out title like below:
<title><?php if (isset($_GET["category_id"])) { echo $title; echo " |"; }
if (isset($_GET["article_id"])) { echo $title; echo " |"; }
if (isset($_GET["user_id"])) { echo $title; echo " |"; } ?> mypage.com</title>
*
This is result:
on category.php?category_id=1
Page title is: "Category 1 | mypage.com"
on article.php?article_id=1
Page title is: "Article 1 | mypage.com"
on users.php?user_id=1
Page title is: "Username 1 | mypage.com"
*
My question is, I am wondering if I over load the database with this structure?
When I'm on users.php?user_id=1
is php still execute this code?:
//1- connect to categories table if isset get category_id
if (isset($_GET["category_id"])) {
$query = $handler->query("SELECT * FROM categories WHERE category_id = ".$_GET['category_id']." ");
while($r = $query->fetch()) {
$title = $r["title"];
}
}
or because of I'm using if (isset($_GET["category_id"])) {
on the begining I'm not connecting to database and there is no over load to database?