Fluoresce Posted March 16, 2009 Share Posted March 16, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/149614-solved-php-and-meta-tags/ Share on other sites More sharing options...
Daniel0 Posted March 16, 2009 Share Posted March 16, 2009 Yes, you need to declare variables before you can use them, if that's what you're asking. Quote Link to comment https://forums.phpfreaks.com/topic/149614-solved-php-and-meta-tags/#findComment-785656 Share on other sites More sharing options...
Fluoresce Posted March 16, 2009 Author Share Posted March 16, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/149614-solved-php-and-meta-tags/#findComment-785659 Share on other sites More sharing options...
daviddth Posted March 16, 2009 Share Posted March 16, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/149614-solved-php-and-meta-tags/#findComment-785800 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.