- Actually I try to get users data and fetch to Auth database table
data from the database. - This whole thing happened RDBMS. In this case I got some errors.
- When I write this code I got some errors but I could not solve that
errors so kindly check that and tell me why that error occurs and how
to solve that in my code.
CODE
<?php
require_once "Database.class.php";
class User
{
private $conn;
public function __call($name,$arguments){
if(substr($name, 0, 3)== "get"){
return $this->__get($property);
}esleif(substr($name, 0, 3)== "set"){
return $this->__set($name,$arguments[0]);
}
}
public function __call($name, $arguments)
{
//$name="getFirstName"; //ForChecking Purpose
$property = preg_replace("/[^0-9a-zA-Z]/","",substr($name,3));
$property = strtolower(preg_replace('/B([A-Z])/','_$1',$property));
//echo $property; //ForChecking Purpose
if(substr($name,0,3)== "get"){
return $this->__get($property);
}elseif(substr($name, 0, 3) == "set"){
return $this->__set($name,$arguments[0]);
}
}
public static function signup($user, $pass, $email, $phone)
{
$option = [
'cost' => 8,
];
$pass = password_hash($pass, PASSWORD_BCRYPT, $option);
$conn = Database::getConnection();
$sql = "INSERT INTO `Auth` (`username`, `password`, `email`, `phone`, `blocked`, `active`)
VALUES ('$user', '$pass', '$email', '$phone', '0', '1');";
$error = false;
if ($conn->query($sql) === true) {
$error = false;
} else {
// echo "Error:" . $sql . "<br>" . $conn->error;
$error = $conn->error;
}
//$conn->close();
return $error;
}
public static function login($user, $pass)
{
$query = "SELECT * FROM `Auth` WHERE `username` = '$user'";
$conn = Database::getConnection();
$result = $conn->query($query);
if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
if (password_verify($pass, $row['password'])) {
return new User($row['username']);
} else {
return false;
}
} else {
return false;
}
}
public function __construct($username)
{
$this->conn = Database::getConnection();
$this->username = $username;
$sql = "SELECT `id` FROM `Auth` WHERE `username`='$username' LIMIT 1";
$result = $this->conn->query($sql);
if($result->num_rows){
$row = $result->fetch_assoc();
$this->id = $row['id']; //updating from the database
}else{
throw new Exception("Username doesn't exist");
}
//this function helps to set the data from the database
private static function __set($var, $data)
{
if (!$this->conn) {
$this->conn = Database::getConnection();
}
$sql = "UPDATE `users` SET `$var`='$data' WHERE `id` = '$this->id';";
if ($this->conn->query($sql)) {
return true;
} else {
return false;
}
}
//this function helps to retrieve data from the database
private function __get($var)
{
if (!$this->conn) {
$this->conn = Database::getConnection();
}
$sql = "SELECT `$var` FROM `users` WHERE `id` = '$this->id'";
$result = $this->conn->query($sql);
if ($result->num_rows()) {
return $result->fetch_assoc()["$var"];
} else {
return null;
}
}
public function authenticate()
{
}
}
Error Message
But could not understand What is the error is, the error is mentioned curly braces, Pls solve and explain me.
2
Answers
tl;dr
You have an elseif that is misspelled as esleif.
There are a number of things wrong here:
You have 2 __call methods.
You’re defining a __call method that calls __get and __set, which are magic functions…so you don’t need a __call method at all.
In your first __call method, you’re using a property named $property, but that doesn’t exist.
Because you misspelled the elseif, the code is expecting a function names esleif, which then throws off all of your matching { and }
And there are more…
I would seriously advise you take a course on PHP programming, rather than just copy/paste from another site, they are notorious for being incorrect.
HTH
You did not properly close the __construct function:
(i.e. a closing brace is missing)
Please change
to
and there is a typo "esleif" too. (please properly review all code in your class)
Last but not least, please change your query statements to parameterized prepared statements which are resilient against SQL injection.