Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php调用单独的函数文件?_Php_Mysql_Database - Fatal编程技术网

如何使用php调用单独的函数文件?

如何使用php调用单独的函数文件?,php,mysql,database,Php,Mysql,Database,好的,我正在学习使用php编程,似乎所有的教程都没有提到任何关于functions.php文件和单独的db_connect.php文件的内容。我需要帮助,所以我正在尝试建立一个学生管理系统。我做了登录和注销,他们的工作和个人资料页面显示的用户名。但是现在,例如,当我想查看所有提供的课程时,如何从sperate文件中查看这些课程?并在profile.php文件中调用它 functions.php类: <?php class Functions{ priva

好的,我正在学习使用php编程,似乎所有的教程都没有提到任何关于
functions.php
文件和单独的
db_connect.php
文件的内容。我需要帮助,所以我正在尝试建立一个学生管理系统。我做了登录和注销,他们的工作和个人资料页面显示的用户名。但是现在,例如,当我想查看所有提供的课程时,如何从sperate文件中查看这些课程?并在profile.php文件中调用它

functions.php
类:

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

我想用它来打印课程,但我得到了N。我做错了什么?

通常,人们会将数据库连接对象作为参数添加到函数类的构造函数中,而不是像这样使用
全局
调用,您可以像这样重写
函数
类:

<?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
?>

只需在$functions->viewAllCourses()之前删除echo

viewAllCourses
方法中,调用
fetch
,然后尝试
fetch\u all
——在这种情况下,不需要初始的
fetch
。事实上,由于sql不使用任何用户提供的数据,从技术上讲,您不需要准备好的语句。其次,您不需要回显函数,因为您已经在函数内部回显了。请在
$stmt->store_result()之后尝试
var_dump($stmt)
并查看结果
<?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
?>
<?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();
?>