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"); ?>

Edited 3 Years Ago by Dani: Formatting fixed

Print the query and execute it in mysql console/phpmyadmin. There is something wrong with your query. Please use code tags next time, as it will be easy for us to go through your code.

Print the query and execute it in mysql console/phpmyadmin. There is something wrong with your query. Please use code tags next time, as it will be easy for us to go through your code.

Hi what exactly do I need to run in php myadmin? Bit new to this sorry.

Print the query (the one which is giving you the error, line 39 or 40) and execute it in phpmyadmin.

you have a typ at the start

function confirm_query($result_set) {
if (!Sresult_set) {

must be

function confirm_query($result_set) {
if (!$result_set) {

not sure that will anser your queston

This article has been dead for over six months. Start a new discussion instead.