proudmore
@proudmore

Почему ругается на неопределенную переменную?

Здравствуйте. Начинаю писать классы, и вот возник вопрос, почему выдает такую ошибку:
Notice: Undefined variable: username in C:\server\Apache24\htdocs\www\User.php on line 20
При таком коде?
class User{
	public function __construct($id){
		$mysqli = db_connect();
		$req = "SELECT u.ID, u.name, u.age, u.city_id, c.city_name FROM users AS u INNER JOIN cities AS c ON (c.city_id = u.city_id) WHERE u.ID = $id";
		$res = $mysqli->query($req);
		if($res !== false){
			if($res->num_rows == 1 ){
				while($row = $res ->fetch_assoc()){
					$this-> $id = $row['ID'];
					$this-> $username =  $row['name'];
					$this-> $city_name = $row['city_name'];
					$this-> $city_id = $row['city_id'];
					$this-> $age = $row['age'];
				}
			}  else return false;
		} else return false;
	}
	
	public static function GetAllUsers(){
		$mysqli =  db_connect();
		$req = 'SELECT u.id, u.Name, u.age, c.name FROM users AS u INNER JOIN cities AS c ON (c.city_id = u.city_id) ORDER BY u.ID';
		$result=$mysqli->query($req);
		if($result !== false) {
			if($result->num_rows !== 0) {
				return $result;
			} else echo 'table users is empty';
		} else echo 'something went wrong';
	}
	
	private $username;
	private $city_name;
	private $city_id;
	private $id;
	private $age;
	
	private function db_row_update($col_name, $value){
		$mysqli =  db_connect();
		$value = $mysqli->real_escape_string($value);
		$request = "UPDATE TABLE users SET $col_name=$value WHERE ID=$this->$id";
		$result = $mysqli->query($request);
		if($result){
			if($col_name == 'city_id'){
				$result->free();
				$request = 'SELECT city_name FROM cities WHERE city_id = $value';
				$result = $mysqli->query($request);
				while($row = $result->fetch_row()){
					$city_name = $row[0];
					break;
				}
				return $city_name;
			} else return true;
		} else return false;
	}
	

	public function set_name($value){
		$this->$username = $value;
		if(db_row_update('name', $value)) return true;
		else return false;
		
	}
	public function set_age($value){
		$this->$age = $value;
		if(db_row_update('age', $value)) return true;
		else return false;
	}
	public function set_city($value){
		$this->$city_id = $value;
		$name = db_row_update('city_id', $value);
		if(is_string($name)){
			$this->$city_name = $name;
			return true;
		} else return false;
		
	}
	public function getName(){
		return $this->$username;
	}
	public function getAge(){
		return $this->$age;
	}
	public function getCityName(){
		return $this->$city_name;
	}
	public function getID(){
		return $this->$id;
	}
	public function getCityId(){
		return $this->$city_id;
	}
}

Вот эта 20 строка, в конструкторе:
$this-> $username = $row['name'];
Что не так?
  • Вопрос задан
  • 177 просмотров
Решения вопроса 1
BuriK666
@BuriK666
Компьютерный псих
$this->username = $row['name'];
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы