0

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"); ?>
6
  • 2
    without getting more context, it's hard to tell. Can you please give some more detail, like where this function is defined, and where you are calling it? Commented Feb 19, 2011 at 8:04
  • is function is defined in current scope ? are you including file in which function is defined ? Commented Feb 19, 2011 at 8:05
  • the actual function was created in a functions file and then included into the main file where it was called. Commented Feb 19, 2011 at 8:13
  • check the path of included file in include_ince. Or use require_once, it will give you error message if file does not exists. Commented Feb 19, 2011 at 8:15
  • @Gaurav I am actually using require_once and it gave me the error message quoted in the OP Commented Feb 19, 2011 at 8:17

2 Answers 2

3

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;
        }

        ?>
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.