我有这个外部文件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
'$ connect'不会返回'Mainconnect()'函数 – devpro
'mysqli_real_escape_string'后应该使用'stripcslashes'吗?密码也应该散列。 – chris85
如果mainConnect()中存在'$ connect',则返回它。 – devpro