Ok, so I'm just learning to program using php, and it seems like all the tutorials don't mention anything about having a functions.php file and a seperate db_connect.php file. I need help, so I'm trying to build a student management system. I did the login and logout and they work and the profile page shows the user's name. But now when for example I want to view all the courses offered, how can I do that from a sperate file? and call it in the profile.php file.

the functions.php class:

<?php


    class Functions{
            private $link;

            public function __construct(){
                //require_once dirname(__FILE__) returns the path of the current directory


                global $link;
                $this->link = $link;
            }

            public function viewAllCourses(){
             $stmt = $this->link->prepare("SELECT * FROM courses where active = 1");
                    $stmt->execute();
                    $stmt->fetch();


                $stmt->store_result(); 
                if($stmt->num_rows > 0){
                    while($row = $stmt->fetch_assoc()){
                        echo $row['course_num'] . "<br>";
                        echo $row['professor_teaching'] . "<br>";
                        echo $row['name'] . "<br>";
                    }
                }
            }
    }
?>

then this is the segement where I try to call it in the profile.php file:

<?php
    require('functions.php');
    $functions = new Functions();

    echo $functions->viewAllCourses();
?>

i want it to print the courses, but I get nth. What am I doing wrong?

2 Answers

0
RamRaider On Best Solutions

Typically one would add the database connection object as an argument to the constructor of the Function class rather than using a global call as you did so you could rewrite the Functions class like so:

<?php

    class Functions{
        private $link;

        public function __construct( $link ){
            $this->link = $link;
        }

        public function viewAllCourses(){

            $sql='select * from `courses` where `active` = 1';
            $res = $this->link->query( $sql );

            if( $res ){
                while( $rs=$res->fetch_object() ){
                    printf(
                        '%s<br />%s<br />%s<br />',
                        $rs->course_num,
                        $rs->professor_teaching,
                        $rs->name
                    )
                }
                return true;
            }
            return false;
        }

    } //end class
?>

The viewAllCourses method does not require a prepared statement as there is no user supplied data being used and thus no way to tamper with the sql at runtime so a simple query should suffice.

To call the class

<?php
    /* include the necessary files */
    require 'db_connect.php';
    require 'functions.php';

    /* construct the db connection to supply as an argument to the Functions class */
    $db=new db_connect();

    /* invoke the functions class */
    $fn = new Functions( $db );

    /* the `viewAllCourses` method echoes the output */
    $fn->viewAllCourses();
?>
-1
wmarx On

simply delete the echo before $functions->viewAllCourses();