Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How do you include php in your meta tags?

Featured Replies

Does anyone know how I would fix this code? I am trying to populate my meta tags from a database but I can't seem to get it working properly. Any help would be appricated.

 

<?php
if (isset($info)){
$subject_set = mysql_query("Select * FROM movies WHERE title like '%".$info."%' and media = '".$med."'", $connection);}
else{
$subject_set = mysql_query("Select * FROM movies", $connection);
}
if (!$subject_set){
die("Database connection failed: " . mysql_error());
}
 while ($subject = mysql_fetch_array($subject_set)){

$pgKeywords = $_GET['keywords'];
$pgDesc = $_GET['page_description'];
$tpTitle = $_GET['title'];?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="<?php echo $pgKeywords?>">
<meta name="description" content="<?php echo $pgDesc?>">
<meta name="title" content="<?php echo $tpTitle?> Review on MRZ">
<title><?php echo $tpTitle?> Review on MRZ</title>
<?php } ?>

Edited by johnmcg0000

<?php echo $pgKeywords?>

<?php echo $pgDesc?>

<?php echo $tpTitle?>

 

Try to put semicolons ( ; ) after the variables.

 

<?php echo $pgKeywords;?>

<?php echo $pgDesc;?>

<?php echo $tpTitle;?>

 

Léon

This is not the way to approach what you're trying to do. Research into object oriented programming, specifically PHP, and then try to get an understanding of something called Model-View-Controller (MVC). At the very minimum - mysql is discouraged, you should use PHPs PDO or a mysqli database class.

 

Also, it looks like you're trying to print meta tags in a loop - so you'll end up with several descriptions, etc.. This does not make sense.

 

Edit: As a starting point, try running this. I've just written it out so might not work perfectly for you. Again, it would be better to use PDO, but this is somewhat simpler and closer to your original.

 

<?

define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'movie_database');

class Database{

   private $con, $result;

   public function __construct($host, $user, $pass, $db){
       $this->con = @new mysqli($host, $user, $pass, $db);
       if ($this->con->connect_error){
           die ("Database connection error: " . $this->con->connect_error);
       }
   }

   public function query($sql){
       $this->result = @$this->con->query($sql);
       if ($this->result === FALSE){
           die ("Database query error: " . $this->con->error);
       }
   }

   public function get_result(){
       return $this->result;
   }

   public function close(){
       $this->con->close();
   }

}

$database = new Database(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$database->query("SELECT * FROM movies");

if ($database->get_result() !== FALSE){

   while ($movie = mysqli_fetch_array($database->get_result())){

       //You should have a Movie class rather than relying on an array
       echo "Title: {$movie['title']}<br />\nKeyword: {$movie['keywords']}<br /><br />";

   }

}

$database->close();

Edited by andyl

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.