Hello everyone, I'm currently building a cms system and have hit a brick wall in relation to the error I'm getting below. Any help would be greatly appreciated.
Thanks James.
The error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\widget_corp\includes\functions.php on line 41
The functions.php:
<?php
// This is the place to store basic functions.
function confirm_query($result_set) {
if (!Sresult_set) {
die ("database query failed: " . mysql_error());
}
}
function get_all_subjects() {
global $connection;
$query = "SELECT*
FROM subjects
ORDER BY position ASC";
$subject_set = mysql_query($query, $connection);
confirm_query($subject_set);
return $subject_set;
}
function get_pages_for_subject($subject_id){
global $connection;
$query ="SELECT*
FROM pages
WHERE subject_id ={$subject_id}
ORDER BY position ASC";
$page_set = mysql_query($query, $connection);
confirm_query($page_set);
return $page_set;
}
function get_subject_by_id($subject_id) {
global $connection;
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE id=" . $subject_id ." ";
$query .="LIMIT 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
//: REMEMBER
// If no rows are returned, fetched array will return false
**** ERROR LINE REFERED ABOVE****if ($subject = mysql_fetch_array($result_set)) {
return $subject;
} else {
return NULL;
}
}
function get_page_by_id($page_id) {
global $connection;
$query = "SELECT * ";
$query .= "FROM pages ";
$query .= "WHERE id=" . $page_id ." ";
$query .="LIMIT 1";
$result_set = mysql_query($query,$connection);
confirm_query($result_set);
//: REMEMBER
// If no rows are returned, fetched array will return false
if ($spage = mysql_fetch_array($result_set)) {
return $page;
} else {
return NULL;
}
}
?>
Not sure if its needed but here is the main content php file whichs calls the functions file.
content.php
<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
if (isset($_GET['subj'])){
$sel_subject = get_subject_by_id($GET['subj']);
$sel_page = NULL;
} elseif (isset($_GET['page'])){
$sel_subject = NULL;
$sel_page = get_page_by_id($_GET['page']);
} else {
$sel_subject = NULL;
$sel_page = NULL;
}
?>
<?php include("includes/header.php"); ?>
<table id="structure">
<tr>
<td id="navigation">
<ul class="subjects">
<?php
$subject_set = get_all_subjects();
while ($subject = mysql_fetch_array($subject_set)) {
echo "<li";
if ($subject["id"] == $sel_subject['id']) {
echo " class=\"selected\"";
}
echo "><a href=\"content.php?subj=" .urlencode($subject["id"])
."\">{$subject["menu_name"]}</a></li>";
$page_set = get_pages_for_subject($subject["id"]);
echo"<ul class=\"pages\">";
while ($page = mysql_fetch_array($page_set)) {
echo "<li";
if ($page["id"] == $sel_page['id']) {
echo " class=\"selected\"";
}
echo "><a href =\"content.php?page=" . urlencode($page["id"]) ."\"> {$page["menu_name"]}</a></li>";
}
echo "</ul>";
}
?>
</ul>
</td>
<td id="page">
<?php if (!is_null($sel_subject)) {//subject selected ?>
<h2> <?php echo $sel_subject['menu_name']; ?></h2>
<?php } elseif (!is_null($sel_page)) {//page selected ?>
<h2> <?php echo $sel_page['menu_name']; ?></h2>
<div class="page-content">
<?php echo $sel_page['content']; ?>
</div>
<?php } else { // nothing selected ?>
<h2> Select a subject or page to edit </h2>
<?php } ?>
<br />
</td>
</tr>
</table>
<?php require("includes/footer.php"); ?>