How to create a web page that’s title text and Description text coming directly through MySQL database.
Web page Title and Description are one of the most useful things to create a search engine friendly web page and when we have create them dynamically then they will be more easy to manage and changeable because they all come from MySQL database records. So in this tutorial we are creating a sample php page with complete dynamic page title and Description. So here is the complete step by step tutorial for PHP Dynamically set page Title-Description from MySQL db data.
PHP Dynamically set page Title-Description from MySQL db data.
Code for dynamic-title-description-page.php .
<!doctype html> <html> <head> <meta charset="utf-8"> <title><?php getpagetitle(); ?></title> <meta name="description" content="<?php getpagedescription(); ?>"> </head> <body> </body> </html> <?php function getpagetitle() { include 'dbconfig.php'; $meta_data = "SELECT * FROM page_data WHERE id=1"; $meta_info = mysql_query( $meta_data, $conn ); while($data = mysql_fetch_array($meta_info)) { echo $data['title']; } } function getpagedescription() { include 'dbconfig.php'; $meta_data = "SELECT * FROM page_data WHERE id= 1"; $meta_info = mysql_query( $meta_data, $conn ); while($data = mysql_fetch_array($meta_info)) { echo $data['description']; } } ?>
Code for dbconfig.php file.
<?php //Define your host here. $hostname = "localhost"; //Define your database username here. $username = "root"; //Define your database password here. $password = ""; //Define your database name here. $dbname = "test"; $conn = mysql_connect($hostname, $username, $password); if (!$conn) { die('Could not connect: ' . mysql_error()); } mysql_select_db($dbname, $conn); ?>
Screenshot: