为什么我收到此错误的任何想法?
解析错误:语法错误,意外' $ config' (T_VARIABLE),期望第10行的D:\ xampp \ htdocs \ Server \ User-Side \ Resources \ Php \ SQL \ User.php中的函数(T_FUNCTION)
<?php
class User {
$config = p_ini_file('../../../Config.ini');
function addUser($ip, $userdata) {
$data = explode($user_data);
//Connect to the MYSQL server (Locally hosted at the moment)
$SQL = new SQL("127.0.0.1", $config['username'], $config['password']);
$IP = stripslashes($ip);
$IP = mysql_real_escape_string($ip);
$data[0] = stripslashes($data[0]);
$data[0] = mysql_real_escape_string($data[0]);
$data[1] = stripslashes($data[1]);
$data[1] = mysql_escape_string($data[1]);
$data[2] = stripslashes($data[2]);
$data[2] = mysql_escape_string($data[2]);
//Select MYSQL Database
$SQL->selectDatabase($config['dbname']);
//Check through SQL Databse for username & password match
$SQL->query("SELECT * FROM users WHERE ip='$ip'");
$rows = $SQL->getRows();
if($rows == 1) {
//User exists. Error.
echo "Error: User exists already.";
} else {
//User doesn't exist. Add.
$SQL->query("INSERT INTO users (ip, os, machine_name, java_v) VALUES ('$ip', '$data[0]', '$data[1]', '$data[2]')");
}
$SQL->close();
}
function removeUser($ip) {
//Connect to the MYSQL server (Locally hosted at the moment)
$SQL = new SQL("127.0.0.1", $config['username'], $config['password']);
//Select MYSQL Database
$SQL->selectDatabase($config['dbname']);
//Check through SQL Databse for username & password match
$SQL->query("SELECT * FROM users WHERE ip='$ip'");
$rows = $SQL->getRows();
if($rows == 1) {
//User exists. Remove.
$SQL->query("REMOVE FROM users WHERE ip='$ip'");
} else {
//User doesn't exist. Error.
echo "Error: User doesn't exists.";
}
$SQL->close();
}
function heartBeat() {
}
}
?>
答案 0 :(得分:3)
此行不正确:
$config = p_ini_file('../../../Config.ini');
您要分配一个变量,但它在一个类中,因此需要将其定义为该类的属性。这意味着使用其中一个关键字public
,protected
或private
添加该属性。
有关属性的更多信息:
http://php.net/manual/en/language.oop5.properties.php
此外,您无法在那里调用函数。