Leahcim
Leahcim

Reputation: 41919

Getting Undefined function error message for a function that's defined

I'm getting the following error message

Fatal error: Call to undefined function get_subject_by_id() in /hsphere/local/home/c263430/prupt.com/content.php on line 25

This is the function at line 25

 $sel_subject = get_subject_by_id($sel_subj);

This is the function it refers to. How can it be "undefined" as the error message says? Can anyone help?

function get_subject_by_id($subject_id) {
    global $connection;
    $query = "SELECT * ";
    $query .= "FROM subjects ";
    $query .= "WHERE id='" . mysql_real_escape_string($subject_id) ."' ";
                       // note the quotes and escaping wrapper
    $query .= "LIMIT 1";
    $result_set = mysql_query($query, $connection);
    confirm_query($result_set);
    // if no rows are returned, fetch array will return false
    if ($subject = mysql_fetch_array($result_set)) {
        return $subject;
    } else {
        return NULL;
    }
}

Below is the whole code of the page where the function is called

<?php

    //1.Create a database connection
    $connection = mysql_connect("98.130.0.87", "User", "Pass");
    if (!$connection) {
        die("Database connection failed: " . mysql_error());
    }
    $db_select = mysql_select_db("C263430_testorwallo" ,$connection);
    if (!$db_select) {
        die("Database selection failed: " . mysql_error());
    }

    ?>

    <?php
        if (isset($_GET['subj'])){

        $sel_subject = get_subject_by_id($_GET['subj']);
        $sel_pg = 0;
        $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 require_once("includes/functions.php"); ?>
    <?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)) {
                        <h2><?php echo $sel_subject['menu_name']; ?></h2>
                        <?php } else if (!is_null($sel_page)) {
                        <h2><?php echo $sel_page['menu_name]; ?></h2>
                        <?php } else {
                        <h2> Select a subject or page to edit </h2>
                        <?php } ?>
                    </td>
                </tr>
                </table>
    <?php include("includes/footer.php"); ?>

Upvotes: 0

Views: 3572

Answers (2)

user1867466
user1867466

Reputation: 1

Obviously this has already been answered, but hopefully this will help someone else. I was experiencing this same issue, and it was because somehow x <?php was the first line of my file, instead of just <?php after I had updated my repository.

Upvotes: 0

Gaurav
Gaurav

Reputation: 28755

You are calling the function before is has been included.

You must use require_once before calling this function.

<?php 
        require_once("includes/functions.php"); // include file here

        if (isset($_GET['subj'])){

        $sel_subject = get_subject_by_id($_GET['subj']);
        $sel_pg = 0;
        $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;
        }

        ?>

Upvotes: 3

Related Questions