Jump to content

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 :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.