Jump to content

[SOLVED] PHP and Meta Tags


Fluoresce

Recommended Posts

I've put my meta descriptions, titles and keywords in a database. I want to dynamically insert them.

 

In the body of my page, I have the following code, which selects the metadata:

 

<?php

$select = "SELECT description, keywords, title FROM meta WHERE id=12";
$query = mysql_query($select, $conn) or trigger_error("SQL", E_USER_ERROR);
mysql_num_rows($query);
$row = mysql_fetch_assoc($query);

?>

 

But when I try to echo the metadata in the header, it doesn't work:

 

<title><?php echo $row['title']; ?></title>

 

Can this not be done? Must the variables be declared above the metadata?

Link to comment
https://forums.phpfreaks.com/topic/149614-solved-php-and-meta-tags/
Share on other sites

Thanks for the quick reply.

 

I was sure that I read somewhere that, being a loosely written programming language, and since the whole page is read before being sent back by the engine, variables could be used anywhere, even before being declared. I was wrong, then.  :-[

It does not mean you cant do what you need. I use meta tags from the database here & they work fine, but the work is done in the <head> section of the page, not the <body>

 

i.e.

In the header of each page I have the following:

<head>
<?php
$bookname='geraldgm';
include 'dbconnect.php';
?>
</head>

 

then in dbconnect.php it has the following bit of code:

 

$sql = "SELECT * FROM booklist where ShortName='$bookname'";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);

// bits cut out

echo '<title>'.$row['HeaderName']."</title>\n";
echo '<meta name="description" content="'.($row['MetaDescription']).'">'."\n";
echo '<meta name="keywords" content="'.($row['MetaKeywords']).'">'."\n";

 

It gives me the info I need for the whole page and also throws in the title, and meta tags as a bonus :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.