Jump to content

dynamic metatags with php and mysql


williamh69
Go to solution Solved by Ch0cu3r,

Recommended Posts

hi guys, i have this database table called meta_tags

 

id

keywords

description

title

 

and I have the following code to retrieve the data

<?php

    $id = 1;
  $query=("SELECT title, keywords, description FROM meta_tags WHERE id =$id");

         $result=mysql_query($query);


         while($row=mysql_fetch_array($result,MYSQL_ASSOC))
              {

             $title= $row['title'];
             $keywords= $row['keywords'];
             $description= $row['description'];


echo"<meta name='title' content='$title'>";
echo"<meta name='keywords' content='$keywords'>";
echo"<meta name='description' content='$description'>";
}

 ?>

but I have this error:

 

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\WHB\includes\metags.inc.php on line 10

 

can you guys help me please.... thank you

 

 

Link to comment
Share on other sites

That error usually means the query has failed, due to an error. Is that all your code? Are you connected to mysql?

 

Use mysql_error to see what the error could be.

$result=mysql_query($query) or trigger_error('DB error: ' . mysql_error());
Edited by Ch0cu3r
Link to comment
Share on other sites

hi guys, i change all the code:

<?php
$dbhost = 'localhost';
$dbuser = 'whbweb';
$dbpass = 'andres69';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
if (!isset($page_id))
    $page_id=1;
$sql = 'SELECT  keywords,title,description FROM meta_tags where id=$page_id';

mysql_select_db('whb');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_assoc($retval))
{
    $title= $row['title'];
             $keywords= $row['keywords'];
             $description= $row['description'];


echo"<meta name='title' content='$title'>";
echo"<p>";
echo"<meta name='keywords' content='$keywords'>";
echo"<p>";
echo"<meta name='description' content='$description'></br>";

}

mysql_close($conn);
?>

Now I have the following error:

 

Could not get data: Unknown column '$page_id' in 'where clause'

Link to comment
Share on other sites

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.