PHP在外部文件中调用类中的函数,语法?

时间:2016-08-19 14:56:10

标签: php function class mysqli

我有这个外部文件CreateConnection.php,它有一个类DBController及其以下函数。 createconnection文件没有遇到任何错误。

中的

CreateConnection.php 文件

<?php
class DBController
{
    private $servername = "localhost";
    private $username = "root";
    private $password = "";
    private $database = "mydatabase";

    function mainConnect()
    {
        //call the function connectDatabase to $connect
        $connect = $this->connectDatabase();

       //call the function selectDatabase
        $this->selectDatabase($connect);

        if(!$connect)
        {
            //Otherwise, prompt connection failed
            die("Connection failed: ".mysqli_connect_error());
        }
    }

    function connectDatabase()
    {
        //Create connection
        $connect = mysqli_connect($this->servername, $this->username, $this->password);
        return $connect;
    }

    function selectDatabase($connect)
    {
         //Select database
         mysqli_select_db($connect, $this->database);
    }
}
?>

在此文件中,我包含外部CreateConnection.php,但我的&#39; $ connect &#39;调用mainConnect()函数时遇到许多错误。 Process.php 文件

<?php
    include("CreateConnection.php");
    $DBHandler = new DBController();

    $connect = $DBHandler->mainConnect();

    //Get values from form LoginReminder.php file
    $username = mysqli_real_escape_string($connect, $_POST['username']);
    $password = mysqli_real_escape_string($connect, $_POST['password']);
    //Removes back slashes in input
    $username = stripcslashes($username);
    $password = stripcslashes($password);

    //Query the database for users
    $result = mysqli_query($connect,"select * from tablereminders where username = '$username' and password = '$password' ");

    //Error connection and query
    if (!$result)
    {
        printf("Error: %s\n", mysqli_error($connect));
        exit();
    }
?>

我的语法不正确,因为我在提交表单时遇到了很多错误:

错误

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\Web Php Tutorial\LoginProcess.php on line 8

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\Web Php Tutorial\LoginProcess.php on line 9

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\Web Php Tutorial\LoginProcess.php on line 15

Warning: mysqli_error() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\Web Php Tutorial\LoginProcess.php on line 20

1 个答案:

答案 0 :(得分:7)

return $connect;的末尾添加function mainConnect(){即可。

但为什么你根本不使用OOP版本的MYSQLi?

mysqli, OOP vs Procedural

class DBController
{ 
   private $db;

   private $servername = "localhost";
   private $username = "root";
   private $password = "";
   private $database = "mydatabase";

   function __construct(){
      $this->db = new mysqli($this->servername, $this->username, $this->password);
      $this->db->select_db($this->database);
   }
   function db(){
      return $this->db;
   }

}
$connection = new DBController();

#now instead of  using mysqli_query();
$connection->db()->query('SQL QUERY');

一个只包含一个类的文件应该将其命名为:

而不是CreateConnection.php更好DBController.php

文件名听起来像包含它时会创建连接,但事实并非如此:)