mysql - PHP destruct function show an error? -
i have error shown in destruct function in class connects data base, tried remove variable in destruct function shows error. here code:
class db_connect { function __construct() { $this->connect(); echo "connected succesfully"; } function __destruct() { $this->close(); } function connect() { require_once __dir__ . '/db_config.php'; $con = mysqli_connect(db_server, db_user, db_password, db_database) or die(mysqli_error()); if($con){ echo "connected succesfully"; } return $con; } function close() { mysqli_close($con); } }
and there error message:
mysqli_close() expects parameter 1 mysqli
in connect method not assigning mysqli
object instance variable.
in __destruct
$con
undefined/null explains error message.
require_once __dir__ . '/db_config.php'; class db_connect { private $conn; function __construct() { $this->connect(); } function __destruct() { $this->close(); } function connect() { $this->conn = mysqli_connect(db_server, db_user, db_password, db_database) or die(mysqli_error()); echo "connected succesfully"; } function close() { mysqli_close($this->conn); } }
Comments
Post a Comment