2016-08-19 98 views
3

我有這個外部文件CreateConnection.php有一個類DBController及其以下功能。 createconnection文件不會遇到錯誤。PHP在外部文件中調用類中的函數,語法?

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,但我的 '$連接' 調用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 
+0

'$ connect'不會返回'Mainconnect()'函數 – devpro

+0

'mysqli_real_escape_string'後應該使用'stripcslashes'嗎?密碼也應該散列。 – chris85

+1

如果mainConnect()中存在'$ connect',則返回它。 – devpro

回答

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

文件名聽起來像那麼其會在創建連接其包括但不是這種情況:)

+0

嗯,這是不是一個好習慣使用類的連接先生?有人說最好的方法是使用PDO。 –

+0

這是一個很好的實踐,但MYSQLi本身有類。因此,要構建DBConnector,最好使用它的類版本。 – JustOnUnderMillions

+0

@laurence keith albano更新了我的答案 – JustOnUnderMillions