I am trying to create my own class to handle mysql interactions on a test site I’m making. I can’t get the mysqli_connect() to work within the class declaration. Every time I try to use the class in a script I get this syntax error:
Parse error: syntax error, unexpected ‘(‘, expecting ‘,’ or ‘;’ in C:\xampp\htdocs\autoshop\classes\class_db.inc.php on line 7
Here is the code I have so far:
<?php
class Db{
private $host = 'localhost';
private $db_user = 'username';
private $db_pass = 'password';
private $db_name = 'dbname';
private $con = mysqli_connect($this->host, $this->db_user, $this->db_pass, $this->db_name) or die('failed to connect');
public function valid_login($login_user, $login_pass){
if(isset($login_user) && isset($login_pass) && ($login_user !== "") && ($login_pass !== "")){
include('./includes/functions.php');
$clean_lu = mysqli_real_escape_string($this->con, $login_user);
$clean_lp = mysqli_real_escape_string($this->con, $login_pass);
$login_query = "SELECT hash FROM users WHERE user_name='$clean_lu'";
$login_results = mysqli_query($this->con, $login_query) or die('bad query');
$login_hash = mysqli_fetch_array($login_results);
if(isset($login_hash[0])){
if(check_hash($clean_lp, $login_hash[0])){
return true;
}else{
return false;
}
}else{
return false;
}
}
}
mysqli_close($this->con);
}
?>
I keep looking at that line and I can’t find any bad syntax.
I apologize if this is a really simple solution that’s right in front of my face, I’m pretty new at this whole developer thing.
You cannot initialize object’s member with result of function call. Perform this in your constructor instead.
Also, where is
mysqli_close($this->con);located? It doesn’t belong to any of methods.Btw, any reason to close connection manually at all?