0

This one is very simple, but I've gone in so many circles today I'm completely lost!

I'm trying to grab the correct page title and page content, based upon a variable I set at the top of the page. ie:

<?php 
$pageIDn = 2;
include($_SERVER['DOCUMENT_ROOT'] .'/includes/functions.php');
content($pageIDn); // the function that queries the DB
include($_SERVER['DOCUMENT_ROOT'] .'/includes/header.php'); ?>

Within functions then I have this function/query:

function content($pageID){
    $query = mysql_query("SELECT * FROM pages WHERE id = 1");
    $title = $query['title'];
    $content = $query['content'];
}

And then in the relevant locations in the file I'm trying to echo the variables ie:

<h2><?php echo $content; ?></h2>

I know I've made a very stupid mistake here somewhere... just not sure where.

Alexander Wigmore
  • 3,157
  • 4
  • 38
  • 60
  • Is there an error on the page? – Muhambi Jan 06 '13 at 21:51
  • 1
    Two mistakes: Not enabling error_reporting, and not looking at the manual examples for [`mysql_query`](http://php.net/mysql_query) right away. (Also, still using mysql_query is a mistake in itself. But it's entirely your decision to use the more cumbersome api.) – mario Jan 06 '13 at 21:52

2 Answers2

2

You have three big errors:

  1. You forgot to put your $pageID in your query:

    $query = mysql_query("SELECT * FROM pages WHERE id = {$pageID}");
    
  2. ...to fetch your results:

    $row = mysql_fetch_assoc($query);
    $title = $row['title'];
    $content = $row['content'];
    
  3. ...and return and capture your output:

    return array($title, $content);
    
    list($title, $content) = content($pageIDn);
    

Stack Overflow:

$pageIDn = 2;
include($_SERVER['DOCUMENT_ROOT'] .'/includes/functions.php');
list($title, $content) = content($pageIDn); // the function that queries the DB
include($_SERVER['DOCUMENT_ROOT'] .'/includes/header.php'); ?>

function content($pageID){
    $query = mysql_query("SELECT * FROM pages WHERE id = {$pageID}");
    $row = mysql_fetch_assoc($query);
    $title = $row['title'];
    $content = $row['content'];
    return array($title, $content);
}

Also, Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • also you have to set the $content variable as a global variable to use it outside the function too. – Sevag Akelian Jan 06 '13 at 21:57
  • Actually I don't because it is in the array that is returned by ` content()` – John Conde Jan 06 '13 at 21:59
  • Thank you very much - it's all so simple when the glaring mistakes are pointed out! Very appreciated. – Alexander Wigmore Jan 06 '13 at 22:00
  • Also regarding the depreciated mysql_*, I wasn't even aware! I've basically hashed together a basic CMS from this tutorial: http://www.melvinwalls.com/2011/01/creating-a-basic-cms-with-php-and-mysql-part-1/ but using it in quite a different way. – Alexander Wigmore Jan 06 '13 at 22:03
1

You first mistake is forget about MYSQL_*. Use PDO Second, you said you wanted to grab $pageIDn = 2, but you did not use at all.

function content($pageID){
$query = mysql_query("SELECT * FROM pages WHERE id = ".$pageIDn." ");
$row = mysql_fetch_array($query);
$title = $query['title'];
$content = $query['content'];
 } 
Rocks
  • 511
  • 1
  • 6
  • 19