November 28, 201213 yr 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 November 28, 201213 yr by johnmcg0000
November 28, 201213 yr <?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
November 28, 201213 yr 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 November 28, 201213 yr by andyl
Create an account or sign in to comment