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

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

里面的CreateConnection.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,但是我的’$connect’在调用mainConnect()函数时遇到了很多错误.

Process.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值